0% found this document useful (0 votes)
46 views

Java Assignment

The document contains 8 Java programs that demonstrate various concepts: 1. A program that counts the number of lines in files passed as command line arguments. 2. A program that starts multiple threads by passing an integer as a command line argument. 3. A program that demonstrates setting thread priority by starting 3 threads with different priorities. 4. A program that converts text in a file to lowercase. 5. A program that converts miles to kilometers using a GUI. 6. A program that moves a label using buttons and keyboard arrows. 7. A program that simulates a traffic light using radio buttons to change color. 8. A program that finds the average of

Uploaded by

mr.x
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Java Assignment

The document contains 8 Java programs that demonstrate various concepts: 1. A program that counts the number of lines in files passed as command line arguments. 2. A program that starts multiple threads by passing an integer as a command line argument. 3. A program that demonstrates setting thread priority by starting 3 threads with different priorities. 4. A program that converts text in a file to lowercase. 5. A program that converts miles to kilometers using a GUI. 6. A program that moves a label using buttons and keyboard arrows. 7. A program that simulates a traffic light using radio buttons to change color. 8. A program that finds the average of

Uploaded by

mr.x
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ASSIGNMENT 3

[JAVA]

SAN K
1.Line Count
import java.io.*;
public class Linecount
{
public static void main(String[] args) throws Exception
{
int num;
for(int i=0;i<args.length;i++)
{
int count=1;
FileInputStream s1=new FileInputStream(args[i]);
num=s1.read();
while(num != -1)
{
if(num==13)
{
num=s1.read();
if(num==10)
count++;
else
continue;
}
num=s1.read();
}
System.out.println(args[i]+"--------"+count);
}
}
}

2.Thread with args


class T1 extends Thread
{
public void run()
{
System.out.println("Hello,I am "+this.getName());
}
}
public class ThreadWithCmdLine
{
public static void main(String[] args) {
for(int i=0;i<Integer.parseInt(args[0]);i++)
{
new T1().start();
}
}
}
3.Thread Priority
class Thread2 extends Thread
{
public void run()
{
System.out.println("Hello,I am "+this.getName());
}
}
public class ThreadPriority
{
public static void main(String[] args) {
Thread2 t1=new Thread2();
Thread2 t2=new Thread2();
Thread2 t3=new Thread2();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t3.start();
}
}

4.Lower case conversion


import java.io.*;
public class LowerConversion
{
public static void main(String[] args) throws Exception
{
int num;
FileInputStream fis=new FileInputStream("e:\\2.txt");
FileOutputStream fos=new FileOutputStream("e:\\3.txt");
num= fis.read();
while(num!=-1)
{
if(num>=65 && num<=90)
num+=32;
fos.write(num);
num=fis.read();
}
System.out.println("Written Successfully");
}
}
5.Conversion
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Conversion {

public static void main(String[] args) {


JFrame f = new JFrame("Conversion");
Label l1 = new Label("Miles");
Label l2 = new Label("Kilometers");
TextField t1 = new TextField(7);
TextField t2 = new TextField(7);
Button b = new Button("Convert!");
f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b);
f.setLayout(new FlowLayout());
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Double d = Double.parseDouble(t1.getText());
Double d1 = 1.609 * d;
t2.setText(d1+"");
}
}
);
f.setResizable(false);
f.setSize(300, 150);
//f.pack();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

6.Button
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Move extends JFrame implements ActionListener,KeyListener
{
JSeparator js1;
JLabel l1;
JPanel p2;
String str[]={"UP","DOWN","LEFT","RIGHT"};
Move()
{
ImageIcon i1=new ImageIcon("e://1//2.png");
js1=new JSeparator();
p2=new JPanel();
l1 = new JLabel();
setSize(500,400);
setVisible(true);
setResizable(false);
setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
p2.setBounds(50,0,400,50);
l1.setIcon(i1);
l1.setBounds(100,100,i1.getIconWidth(),i1.getIconHeight());
add(l1);
js1.setBounds(0, 50, 500, 2);
add(js1);
add(p2);
p2.setLayout(new GridLayout(1,4,10,0));
for(int i=0;i<str.length;i++)
{
JButton b1=new JButton(str[i]);
b1.setSize(45, 30);
b1.addActionListener(this);
p2.add(b1);
}
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("UP"))
{
if(l1.getY() != 50)
l1.setBounds(l1.getX(),l1.getY()-25, 100, 100);
}
else if(e.getActionCommand().equals("DOWN"))
{
if(l1.getY() != 275)
l1.setBounds(
l1.getX(),l1.getY()+25, 100, 100);
}
else if(e.getActionCommand().equals("RIGHT"))
{
if(l1.getX() != 375)
l1.setBounds(l1.getX()+25,l1.getY(), 100, 100);
}
else if(e.getActionCommand().equals("LEFT"))
{
if(l1.getX() != 0)
l1.setBounds(l1.getX()-25,l1.getY(), 100, 100);
}
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
switch(keyCode){
case 38:
if(l1.getY() != 50)
l1.setBounds(l1.getX(),l1.getY()-25, 100, 100);
break;
case 40:
if(l1.getY() != 275)
l1.setBounds(l1.getX(),l1.getY()+25, 100, 100);
break;
case 37:
if(l1.getX() != 0)
l1.setBounds(l1.getX()-25,l1.getY(), 100, 100);
break;
case 39 :
if(l1.getX() != 375)
l1.setBounds(l1.getX()+25,l1.getY(), 100, 100);
break;
}

}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
}
public class Event {
public static void main(String[] args) {
new Move();
}
}

7.Traffic LIght
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Light extends JFrame implements ActionListener
{
JRadioButton rb1,rb2,rb3;
ButtonGroup b1=new ButtonGroup();
JLabel l1;
JPanel p1=new JPanel();
Light()
{
super("TRAFFIC LIGHT");
rb1=new JRadioButton();
rb2=new JRadioButton();
rb3=new JRadioButton();
l1=new JLabel("");
setSize(400,250);
setLayout(null);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
rb1.setText("RED");
add(rb1);
rb1.addActionListener(this);
rb1.setBounds(50, 40, 93, 23);
rb2.setText("YELLOW");
add(rb2);
rb2.setBounds(50, 80, 93, 23);
rb2.addActionListener(this);
rb3.setText("GREEN");
add(rb3);
rb3.setBounds(50, 120, 93, 23);
rb3.addActionListener(this);
b1.add(rb1);
b1.add(rb2);
b1.add(rb3);
add(l1);
l1.setBounds(190, 10, 180, 30);
add(p1);
p1.setBounds(190, 40, 190, 110);
}
public void actionPerformed(ActionEvent e)
{
if(rb1.isSelected())
{
l1.setText("STOP");
p1.setBackground(Color.red);
}
else if(rb2.isSelected())
{
l1.setText("READY");
p1.setBackground(Color.yellow);
}
else if(rb3.isSelected())
{
l1.setText("G0");
p1.setBackground(Color.green);
}
}
}
public class TrafficLight
{
public static void main(String[] args) {
new Light();
}
}

8.Thread to perform operations


import java.util.Scanner;
class Thrd2 extends Thread
{
int num;
Thrd2(int no)
{
num=no;
}
public void run()
{
if(num%5==0)
{
System.out.println("It is divisible by five");
}
}
}
class Thrd1 extends Thread
{
int no,total;
public void run()
{
int num;
System.out.print("Enter the no. of numbers:");
Scanner scan= new Scanner(System.in);
no=scan.nextInt();
for(int i=0;i<no;i++)
{
System.out.printf("Enter the number %d : ",i+1);
num=scan.nextInt();
total+=num;
Thrd2 five = new Thrd2(num);
five.start();
try
{
Thread.sleep(2000);
}
catch(Exception e){}
}
}
}
public class ThreadToPerformOperations
{
public static void main(String[] args)
{
Thrd1 t1=new Thrd1();
t1.start();
try{
t1.join();
}
catch(Exception e){}
new Thread(new Runnable()
{
public void run()
{
int avg = t1.total/t1.no;
System.out.println("Average : "+avg);
}
}).start();
}
}

You might also like