Sie sind auf Seite 1von 29

Q

201 2

ASSIGNMENT OF JAVA

SUBMITTED TO: MOHINDER KUMAR SUBMITTED BY: SOURABH SINGH


Solution 1:

11004901 K3R20B45
SOURABH LOVELY PROFESSIONAL UNIVERSITY 12/9/2012

import java.io.*; import java.util.Scanner;

class Clock { int hours,minutes,seconds; boolean isAM; void getHours() { System.out.print(hours); } void getMinutes() { System.out.print(":"+minutes); } void getSeconds() { System.out.print(":"+seconds+" "); } void getISAM() { if(isAM) System.out.print("PM"); else System.out.print("AM");

} void settime(int h,int m,int s,boolean b) { hours=h; minutes=m; seconds=s; isAM=b; } Clock() { hours=00; minutes=00; seconds=00; isAM=false; } Clock(int h,boolean b) { hours=h; minutes=00; seconds=00; isAM=b; } Clock(int h,int m,int s,boolean b) { hours=h;

minutes=m; seconds=s; isAM=b; } }

class Clc1 { public static void main(String args[]) { Scanner in=new Scanner(System.in); int H,M,S; boolean B; System.out.print("Enter hours:"); H=in.nextInt(); if(H<1||H>12) { System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 1-12 "); H=in.nextInt(); } System.out.print("Enter Minutes:"); M=in.nextInt(); if(M<0||M>59) {

System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 0-59 "); M=in.nextInt(); } System.out.print("Enter Seconds:"); S=in.nextInt(); if(S<0||S>59) { System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 0-59 "); S=in.nextInt(); } System.out.print("Enter true for PM or False for AM :"); B=in.nextBoolean(); System.out.println("\nSetting clock at mid night."); Clock c1=new Clock(); c1.getHours(); c1.getMinutes(); c1.getSeconds(); c1.getISAM(); System.out.println("\n\nSetting minutes and seconds to zero and assign value to hours and isAM."); Clock c2=new Clock(H,B); c2.getHours(); c2.getMinutes(); c2.getSeconds();

c2.getISAM(); System.out.println("\n\nAssign values to all attributes parameterized constructer."); Clock c3=new Clock(H,M,S,B); c3.getHours(); c3.getMinutes(); c3.getSeconds(); c3.getISAM(); System.out.println("\n\nAssign values to all attributes by using settime method."); Clock c4=new Clock(); c4.settime(H,M,S,B); c4.getHours(); c4.getMinutes(); c4.getSeconds(); c4.getISAM(); } }

OUTPUT:

DESCRIPTION: In this program we define a class name Clock which contain 5 methods (getHours,getMinutes,getSeconds,getISAM,settime) and 3 constructer( 1 default and 2 parameterized). Methods show the value of hours, minutes, seconds, AM or PM and, to initialize the value of all attributes respectively. Default constructer set the clock at midnight by initialize all attributes with 00 and parameterized constructer initialize the value of attributes according to the number of parameter passed. Now we create a new class Clc1 in which we enter the value of all attributes by user with the help of Scanner class. If the user enters hours, minutes and seconds outside the range of respective one then we allow to reenter the value of respective one. Now we create the object of Clock class and then invoke all the constructer and method with the help of object.

Solution 2: import java.io.*; import java.util.Scanner;

class Clock {

int hours,minutes,seconds; boolean isAM; void tick() { if(seconds==59) { if(minutes==59) { if(hours==11) { hours=12; minutes=00; seconds=00; if(isAM) isAM=false; else isAM=true ; } else { if(hours==12) { hours=1; minutes=00; seconds=00;

} else { hours=hours+1; minutes=00; seconds=00; } } } else { minutes=minutes+1; seconds=00; } } else seconds=seconds+1; }

void getHours() { System.out.print(hours); } void getMinutes() {

System.out.print(":"+minutes); } void getSeconds() { System.out.print(":"+seconds+" "); } void getISAM() { if(isAM) System.out.print("PM"); else System.out.print("AM"); } void settime(int h,int m,int s,boolean b) { hours=h; minutes=m; seconds=s; isAM=b; } Clock() { hours=00; minutes=00; seconds=00;

isAM=false; } Clock(int h,boolean b) { hours=h; minutes=00; seconds=00; isAM=b; } Clock(int h,int m,int s,boolean b) { hours=h; minutes=m; seconds=s; isAM=b; } public static void main(String args[]) { } }

class Clc2 { public static void main(String args[]) {

Scanner in=new Scanner(System.in); int H,M,S; boolean B; System.out.println("\nEnter hours:"); H=in.nextInt(); if(H<1||H>12) { System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 1-12 "); H=in.nextInt(); } System.out.println("\nEnter Minutes:"); M=in.nextInt(); if(M<0||M>59) { System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 0-59 "); M=in.nextInt(); } System.out.println("\nEnter Seconds:"); S=in.nextInt(); if(S<0||S>59) { System.out.println("You have entered wrong value of hours.\nPlease Enter the value between 0-59 "); S=in.nextInt();

} System.out.println("\nEnter true for PM or False for AM :"); B=in.nextBoolean(); System.out.println("\nSetting clock at mid night."); Clock c1=new Clock(); c1.getHours(); c1.getMinutes(); c1.getSeconds(); c1.getISAM(); System.out.println("\n\nSetting minutes and seconds to zero and assign value to hours and isAM."); Clock c2=new Clock(H,B); c2.getHours(); c2.getMinutes(); c2.getSeconds(); c2.getISAM(); System.out.println("\n\nAssign values to all attributes."); Clock c3=new Clock(H,M,S,B); c3.getHours(); c3.getMinutes(); c3.getSeconds(); c3.getISAM(); System.out.println("\n\nAssign values to all attributes by using settime method."); Clock c4=new Clock(); c4.settime(H,M,S,B);

c4.getHours(); c4.getMinutes(); c4.getSeconds(); c4.getISAM(); System.out.println("\n\nTime after increment one second by using tick() method. "); c4.tick(); c4.getHours(); c4.getMinutes(); c4.getSeconds(); c4.getISAM(); } }

Output:

DESCRIPTION: In this program we define a class name Clock which contain 6 methods (getHours, getMinutes, getSeconds, getISAM, settime, tick) and 3 constructer( 1 default and 2 parameterized). Methods show the value of hours, minutes, seconds, AM or PM and, to initialize the value of all attributes respectively. Tick method increase the value of entered time it invoke.Default constructer set the clock at midnight by initialize all attributes with 00 and parameterized constructer initialize the value of attributes according to the number of parameter passed. We also define an empty main function in Clock class. Now we create a new class Clc1 in which we enter the value of all attributes by user with the help of Scanner class. If the user enters hours, minutes and seconds outside the range of respective one then we allow to reenter the value of respective one. Now we create the object of Clock class and then invoke all the constructer and method with the help of object.

Solution 3: class RmSt { static String oldstr,delstr; public static String removeString(String string,String substring) { oldstr=string; delstr=substring; String newstr; newstr=oldstr.replace(delstr,""); System.out.println("\nEntered string is :"+oldstr); System.out.println("New string is return(newstr); } public static void main(String []args) { String result=removeString("unheunliunum","un"); } } Output: :"+newstr);

Description: In this program we use a public static method named removeString with a given signature. This program remove a substring saved in delstr variable from a string saved in oldstr variable.To perform this task we use a method of String class name replace(). Then in the same class in main function we invoke the removeString method according to given signature.

Solution 4: class A { public int a; private int b; protected int c; int d; public void method1() { System.out.println("This is variable a."); } private void method2() { System.out.println("This is variable b."); } protected void method3() { System.out.println("This is variable c."); } int method4()

{ System.out.println("This is variable d."); return (0); } }

class B { public static void main(String args[]) { A o=new A(); o.a=10; o.b=20; o.c=30; o.d=40; o.method1(); o.method2(); o.method3(); o.method4(); } }

Output:

Description: In this program we use public, private, protected and friendly data members and methods in class A. Then in Class B we create object of class A in class B and invokes all the method and members using object of class A. As we know that we cant invoke the private method weather we have both class in same package or in different package, so it gives error at compile time which is shown in output.

Solution 5: Sol (a): class A { A() { System.out.println("Constructer A."); } }

class B { int b; B() {

System.out.println("Constructer B."); } }

class C extends A { public static void main(String args[]) { B b1=new B() b1.b=10; } }

class Sd { public static void main(String args[]) { C c=new C(); } } Output:

Description:

In this program we create three classes A, B and, C. Class A and B contains a default constructer which invokes themselves respectively. Class A is inherited in class C and define a data member of class B. Now we create a new class Sd in which we create a object of class c which Automatically invokes the default constructer of c class.

Sol (b): class A { int k,l; A(int a,int b) { k=a; l=b; System.out.println("k:"+a); System.out.println("l:"+b); } }

class B { int m; B(int c) { m=c; System.out.println("m:"+c);

} }

class C extends A { B b=new B(8); int i; C(int d,int e,int f) { super(d,e); i=f; System.out.println("i:"+i); } }

class Sdf { public static void main(String args[]) { C c=new C(5,6,7); } }

Output:

Description: In this program we create three classes A, B and, C. Class A and B contains a parameterized constructer whose parameter is assign to the variables of their class respectively. Class A is inherited in class C and defines a data member of class B. Now in class C we define a Parameterized constructer which pass two value to their super class by using super keyword and assign one value to its own class variable. Now we create a new class Sd in which we create object of class c which Automatically invokes the parameterized constructer of c class.

Solution 6: import java.io.*; import java.util.Scanner;

class Rodent { void teeth() { System.out.println("All rodent has teeth 5-20."); } void height() { System.out.println("All rodent has height 10-20cm."); } void weight()

{ System.out.println("All rodent has weight 100-800gm."); } }

class Mouse extends Rodent { void teeth() { System.out.println("All Mouse has teeth 20-30"); } void height() { System.out.println("All Mouse has height 5cm to 10cm."); } void weight() { System.out.println("All Mouse has weight 100 gm to 600gm."); } }

class Gerbil extends Rodent { void teeth() {

System.out.println("All Gerbil has teeth 15-25"); } void height() { System.out.println("All Gerbil has height 3cm to 7cm."); } void weight() { System.out.println("All Gerbil has weight 200 gm to 800 gm."); } }

class Hamster extends Rodent { void teeth() { System.out.println("All Hamster has teeth 19-27."); } void height() { System.out.println("All Hamster has height 10cm to 15cm."); } void weight() { System.out.println("All Hamster has weight 100gm to 300gm.");

} }

class Base { public static void main(String args[]) { int q; String roden[]={"Mouse","Hamster","Gerbil"}; Scanner in=new Scanner(System.in); do { System.out.println("\nEnter 1 for see the Characteristics of mouse. "); System.out.println("Enter 2 for see the Characteristics of Hamster."); System.out.println("Enter 3 for see the Characteristics of Gerbil. "); System.out.println("Enter any number greater than 4 for exit"); System.out.print("\nEnter your choice:"); q=in.nextInt(); switch(q) { case 1: System.out.println("Characterstics of Mouse is:"); Mouse m=new Mouse(); m.teeth(); m.height();

m.weight(); break; case 2: System.out.println("Characterstics of Hamster is:"); Hamster h=new Hamster(); h.teeth(); h.height(); h.weight(); break; case 3: System.out.println("Characterstics of Gerbil is:"); Gerbil g=new Gerbil(); g.teeth(); g.height(); g.weight(); break; } }while(q<=3); } }

Output:

Description: In this program we create a Rodent class which contains method that defines the characteristics of Rodent family. Now we create three more classes named Mouse, Hamster and, Gerbil which contains method that defines theirs specific characteristics respectively and these three clsses extend class Rodent and override the method of class Rodent. Now we create a class Base which create object of classes named Mouse, Hamster and, Gerbil and invokes the methods using their specific object respectively.

Das könnte Ihnen auch gefallen