Commit 558b12df authored by Harsh's avatar Harsh

added java files

parent a2350b91
import java.io.*;
import java.util.*;
public class Problem1 {
public static void main(String[] args) {
int sum = 0;
for (String x : args) {
int y = Integer.parseInt(x);
sum = sum + y;
}
System.out.println(args.length + ", " + sum);
}
}
\ No newline at end of file
import java.io.*;
import java.util.*;
public class Problem2 {
static double mean(int[] a) {
int sum = 0;
for (int x : a)
sum += x;
return sum / a.length;
}
static double stddev(int[] a) {
int sum = 0;
double m = mean(a);
for (int x : a)
sum += Math.pow(x - m, 2);
return Math.sqrt(sum / (a.length - 1));
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
Random randomGenerator = new Random();
for (int i = 0; i < n; i++)
arr[i] = randomGenerator.nextInt(100) + 1; //random no.s between 1-100
System.out.println("Mean = " + mean(arr));
System.out.println("Standard deviation = " + stddev(arr));
br.close();
}
}
\ No newline at end of file
import java.io.*;
import java.util.*;
public class Problem3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input: ");
String inp = br.readLine();
String[] inp_temp = inp.split(",");
String txt = inp_temp[0];
int a = Integer.parseInt(inp_temp[1].trim());
int b = Integer.parseInt(inp_temp[2].trim());
System.out.println("Output: " + txt.substring(a, b+1));
br.close();
}
}
\ No newline at end of file
import java.io.*;
import java.util.*;
import java.text.*;
public class solution {
public static class Runtime implements Runnable {
public void run() {
Date d = new Date();
SimpleDateFormat s = new SimpleDateFormat("HH:mm:ss");
String q = s.format(d);
System.out.printf("\r %s", q);
}
}
public static void main(String[] args) {
Runtime r = new Runtime();
Thread t = new Thread(r);
try {
while (true) {
t.run();
t.sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println(e);
}
}
}
\ No newline at end of file
import java.io.*;
import java.util.*;
public class Problem5 {
static int binarySearch(String[] arr, String key) {
Arrays.sort(arr);
int l = 0;
int h = arr.length - 1;
int mid = 0;
while (l <= h) {
mid = (l + h) / 2;
if (arr[mid].compareTo(key) < 0)
l = mid + 1;
else if (arr[mid].compareTo(key) > 0)
h = mid - 1;
else
return mid;
}
return -1;
}
static String readfile(String filename) throws IOException {
BufferedReader input = new BufferedReader(new FileReader(filename));
StringBuilder s = new StringBuilder();
String line = input.readLine();
while (line != null) {
s.append(line);
s.append("\n");
line = input.readLine();
}
input.close();
return s.toString();
}
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String file = input.readLine();
String file_c = readfile(file);
String splitter = "[ .,?!\n]+";
String[] tokens = file_c.split(splitter);
String[] stop_words = {"and", "the", "is", "in", "at", "of", "his", "her", "him"};
ArrayList<String> al = new ArrayList<String>();
for (String x : tokens)
if (binarySearch(stop_words, x.toLowerCase()) == -1)
al.add(x);
Set<String> s = new HashSet<String>(al);
for (String y : s)
System.out.println(y + " = " + Collections.frequency(al, y));
input.close();
}
}
\ No newline at end of file
import javax.swing.*;
import java.awt.event.*;
public class Problem6 {
public static void main(String[] args) {
//declaraing all components of the window
JFrame window = new JFrame("Program6");
JTextField userid = new JTextField();
JPasswordField passw = new JPasswordField();
JButton login = new JButton("Login");
JButton reset = new JButton("Reset");
JLabel usnm = new JLabel("User Id: ");
JLabel pswd = new JLabel("Password: ");
//setting the size of the components
userid.setBounds(150,50,100,25);
passw.setBounds(150,100,100,25);
login.setBounds(100,200,100,30);
reset.setBounds(100,250,100,30);
usnm.setBounds(50,50,100,25);
pswd.setBounds(50,100,100,25);
window.setSize(300,400);
//action events for the buttons
login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (userid.getText().isEmpty() || passw.getText().isEmpty())
JOptionPane.showMessageDialog(null, "Text fields are empty !", "Message", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(null, "Text fields are not empty !", "Message", JOptionPane.INFORMATION_MESSAGE);
}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userid.setText("");
passw.setText("");
}
});
//adding the components to the main window
window.add(usnm);
window.add(pswd);
window.add(userid);
window.add(passw);
window.add(login);
window.add(reset);
//Some window operations
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setLayout(null);
window.setVisible(true);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment