1) Classes and Objects: i. Write a prog in java with class Rectangle with the data fields width,length,area and color.The length ,width,area are of double type and color is string type.The methods are set_length(),set_width(),set_color and find_area().create two objects of Rectangle and compare their area and color.If area and color both are same for the objects then display "Matching rectangles" otherwise display "Non matching rectangles". ii. Write a java program to overload constructor and method . Program 1a: class Rectangle { double length,width,area; String colour; void SetLength(int i) { length=i; } void SetWidth(int j) { width=j; } void SetColour(String col) { colour=col; } void FindArea() { area=length*width; }
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1) Classes and Objects:i. Write a prog in java with class Rectangle with the data fields width,length,area and
color.The length ,width,area are of double type and color is string type.The methods are set_length(),set_width(),set_color and find_area().create two objects of Rectangle and compare their area and color.If area and color both are same for the objects then display "Matching rectangles" otherwise display "Non matching rectangles".
ii. Write a java program to overload constructor and method
.Program 1a:
class Rectangle{
double length,width,area;String colour;
void SetLength(int i){
length=i;}
void SetWidth(int j){
width=j;}
void SetColour(String col){
colour=col;}
void FindArea(){
area=length*width;}
boolean compare(rectangle a){
if(colour==a.colour&&area==a.area)return true;
elsereturn false;
}}
class RectDemo{
public static void main(String args[]){
rectangle r1 = new rectangle();rectangle r2 = new rectangle();boolean b = false;
4) Exception Handling:i. On a single track two vehicles are running.As vehicles are going on same direction
there is no problem .If the vehicles are running in different direction there is a chance of collision.To avoid collision write a java program using Exception handling.
Program 4a:
class collision{String i,j;collision(String a,String b){
i=a;j=b;
}void check(){
try{
if(i==j){
System.out.println("The two vehicles are moving in same direction, hence no problem");
}else{
throw new Exception("The two vehicles are moving in different directions,so collision occurs");
}}catch(Exception e){
System.out.println(e);}}
}
class exception{public static void main(String args[]){
System.out.println("Thread one is:"+m1.t.isAlive()); System.out.println("Thread two is:"+m2.t.isAlive()); System.out.println("Thread three is:"+m3.t.isAlive()); System.out.println("Thread four is:"+m4.t.isAlive()); System.out.println("Thread five is:"+m5.t.isAlive());
try{
System.out.println("waiting for thread to finish");m1.t.join();m2.t.join();m3.t.join();m4.t.join();m5.t.join();
}catch(InterruptedException e){
System.out.println("main thread interrupted");}
System.out.println("thread one is:"+m1.t.isAlive());System.out.println("thread two is:"+m2.t.isAlive());System.out.println("thread three is:"+m3.t.isAlive());System.out.println("thread four is:"+m4.t.isAlive());System.out.println("thread five is:"+m5.t.isAlive());System.out.println();System.out.println("priority of one:"+m1.t.getPriority());System.out.println("priority of two:"+m2.t.getPriority());System.out.println("priority of three:"+m3.t.getPriority());System.out.println("priority of four:"+m4.t.getPriority());System.out.println("priority of five:"+m5.t.getPriority());System.out.println();
System.out.println(MulThread.last+" is long lasting thread");}
}
Output:
one startednew thread: Thread[one,4,main]two startednew thread: Thread[two,10,main]three startednew thread: Thread[three,7,main]one:0four startednew thread: Thread[four,9,main]three:0five startednew thread: Thread[five,2,main]five:0Thread one is:trueThread two is:trueThread three is:trueThread four is:trueThread five is:truewaiting for thread to finishthree:1one:1five:1two is going to sleepfour is going to sleepfour:0two:0three:2one:2five:2four:1two:1three:3one:3five:3two:2four:2three:4one:4five:4four:3two:3
three exitingfive exitingone exitingtwo:4four:4four exitingtwo exitingthread one is:falsethread two is:falsethread three is:falsethread four is:falsethread five is:false
priority of one:4priority of two:10priority of three:7priority of four:9priority of five:2