You are on page 1of 5

http://www.cs.princeton.edu/introcs http://www.leepoint.net/notes-java/index.html Q1) Design a screen in Java which contains a text box.

If the left mouse button is clicked, convert text in upper case and if the right button is clicked, convert it to lower case.

import java.awt.*; import java.awt.event.*; public class ClickDemo extends Frame implements MouseListener { TextField t1; public ClickDemo(String name) { super(name); t1=new TextField("Example by Kashid"); addMouseListener(this); add(t1); } public void mousePressed(MouseEvent me) { switch(me.getModifiers()) { case InputEvent.BUTTON1_MASK: { t1.setText(t1.getText().toUpperCase()); break; } case InputEvent.BUTTON2_MASK: { System.out.println("You pressed Middle button"); break; } case InputEvent.BUTTON3_MASK: { t1.setText(t1.getText().toLowerCase());;

break; } } } public void mouseReleased(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public static void main(String[]args) { ClickDemo a=new ClickDemo("Click Demo"); a.setSize(150,150); a.setLayout(new FlowLayout()); a.setVisible(true); } }

Output :1. //Using Command Line take an Array of 10 nos and print the Smallest and Largest class ArrNo { public static void main(String args[]){ int arr[]=new int[10]; int i,j,temp; for(i=0;i<10;i++) arr[i] = Integer.parseInt(args[i]); for(i=0;i<10;i++) { for(j=0;j<9;j++) {

if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } System.out.println(Sorted List :); for(i=0;i<10;i++) System.out.println(arr[i]); System.out.println(The Smallest Number is : + arr[0]); System.out.println(The Largest Number is : + arr[9]); } } //Method Overloading class Student { String name; int roll; Student() { name=ABC; roll=123456; } void show() //Method Show() { System.out.println(Name +name+\n+Roll : +roll); } void show(int marks) //Method Show(int) { float avg=(marks/7); System.out.println(Average : +avg); } } class mainMethod extends Student { public static void main(String args[]) { Student std=new Student(); std.show(); //Overloading std.show(511); //Overloading } }

//Method Overriding class marks { private int total; private float avg; marks() { total=511; avg=(total/7); } void show() //Show of marks { System.out.println(Average : +avg); } } class Student extends marks { String name; int roll; Student() { name=ABC; roll=123456; } void show() //Show of Student { System.out.println(Name +name+\n+Roll : +roll); } } class mainMethod { public static void main(String args[]) { Student std=new Student(); std.show(); //Show of marks is overriden by show of Student } }

//Using Command line to Swap two numbers using a function class Swap { public static void main(String args[]) { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); System.out.println(\nOriginal Values); System.out.println(a = +a+ b = + b); swap(a,b); } static void swap(int x, int y) { int temp=x; x=y; y=temp; System.out.println(\nSwap Function Called); System.out.println(x = +x+ y = + y); } }

You might also like