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

Iem Sheet 1copy BW

The document describes a Java program that defines a Triangle class with methods to calculate the area of a triangle and compare the areas of two triangles. It also defines an Equipment class and IFCSManager class to manage equipment objects in an array. The Triangle class takes user input to create two triangle objects and optionally compare their areas. The IFCSManager class allows the user to add, remove, search for, and display equipment objects from an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Iem Sheet 1copy BW

The document describes a Java program that defines a Triangle class with methods to calculate the area of a triangle and compare the areas of two triangles. It also defines an Equipment class and IFCSManager class to manage equipment objects in an array. The Triangle class takes user input to create two triangle objects and optionally compare their areas. The IFCSManager class allows the user to add, remove, search for, and display equipment objects from an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 3

Question 1

Write a class Triangle, which has two member variables base of type int, and height
of type int. Write a constructor which initialises the base and the height of a Triangle
instance. Write a method getArea() that returns the area of the Triangle as a double.
Write a method show(), to print the dimensions and area of the Triangle instance.
Write a method compare(Triangle t1, Triangle t2), which determines compares the
area of two given Triangle objects (hint: recall the Float class compare() method used
in Lab #2). In the main method of the Triangle class, obtain user input for the
Triangle's base and height. If the user wishes to do a comparison, ask for the
dimensions of Triangle t1 and Triangle t2.

Code:

// @author Debsena Sadhukhan, Roll 235


import java.util.Scanner;
class Triangle
{
int base;
int height;
public Triangle(int base,int height)
{
if(base>0 && height>0)
{
this.base=base;
this.height=height;
}
else
{
System.out.println("Error - base / height
cannot be 0 or negative");
System.exit(0);
}
}
public void show()
{
System.out.println("base="+base+"\nheight="+height+"\nare
a="+getArea());
}
public double getArea()
{
return 0.5*base*height;
}
public static int compare(Triangle t1,Triangle t2)
{
if(t1.getArea()>t2.getArea())
return 1;
else if(t2.getArea()>t1.getArea())
return -1;
else
return 0;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x,y;
System.out.println("Triangle 1");
System.out.print("base:");
x=sc.nextInt();
System.out.print("height: ");
y=sc.nextInt();
Triangle t1=new Triangle(x,y);
t1.show();
System.out.println("Want to do comparison?(y/n): ");
char choice=sc.next().charAt(0);
if(choice=='Y'||choice=='y')
{
System.out.println("Triangle 2");
System.out.print("base:");
x=sc.nextInt();
System.out.print("height: ");
y=sc.nextInt();
Triangle t2=new Triangle(x,y);
t2.show();
int cmp=Triangle.compare(t1,t2);
if(cmp==1)
{
System.out.println("t1 is larger");
}
else if(cmp==-1)
{
System.out.println("t2 is larger");
}
else
{
System.out.println("t1 and t2 are equal");
}
}
}
}

Output:

E:\iem\5th Sem\Java\Lab 3>javac Triangle.java


E:\iem\5th Sem\Java\Lab 3>java Triangle
Triangle 1
base:10
height: 10
base=10
height=10
area=50.0
Want to do comparison?(y/n):
y
Triangle 2
base:0
height: 10
Error - base / height cannot be 0 or negative
E:\iem\5th Sem\Java\Lab 3>java Triangle
Triangle 1
base:10
height: -100
Error - base / height cannot be 0 or negative
E:\iem\5th Sem\Java\Lab 3>java Triangle
Triangle 1
base:10
height: 50
base=10
height=50
area=250.0
Want to do comparison?(y/n):
y
Triangle 2
base:5
height: 100
base=5
height=100
area=250.0
t1 and t2 are equal
E:\iem\5th Sem\Java\Lab 3>java Triangle
Triangle 1
base:100
height: 50
base=100
height=50
area=2500.0
Want to do comparison?(y/n):
y
Triangle 2
base:5
height: 100
base=5
height=100
area=250.0
t1 is larger
E:\iem\5th Sem\Java\Lab 3>java Triangle
Triangle 1
base:10
height: -50
Error - base / height cannot be 0 or negative
E:\iem\5th Sem\Java\Lab 3>
Question 2

Implement the Equipment class from the IFCS, according to the following class
diagram:
Equipment
- id : String
- description: String
+ Equipment(id:String, desc:String)
+ getId() : String
+ getDesc() : String
Write an IFCSManager class to maintain an array of Equipment objects, sorted
according to Equipment id. (Hint: refer to Lab #2 Question 2).
The IFCSManager will
add new Equipment instances
remove an Equipment instance specified by its id
given an id, report if the Equipment instance resides in the Lab
display the list of Equipment instances in the Lab.

Code:

// @author Debsena Sadhukhan, Roll 235


import java.util.Scanner;
class Equipment
{
private String id,description;
public Equipment(String id,String description)
{
this.id=id;
this.description=description;
}
public String getId()
{
return id;
}
public String getDesc()
{
return description;
}
}
class IFCSManager
{
private Equipment[] eqpList;
private int c;
public IFCSManager()
{
eqpList = new Equipment[10];
c=0;
}
public boolean insert(Equipment eqp)
{
if(eqp.getId()==null || eqp.getDesc()==null)
{
if(eqp.getId()==null)
{
System.out.println("Error - id cannot be
null");
}
else
{
System.out.println("Error - description cannot
be null");
}
}
else
{
if(c==0)
{
eqpList[c++]=eqp;
}
else if(c<10)
{
int i=0;
while(i<c &&
Integer.parseInt(eqpList[i].getId())<Integer.parseInt(eqp.getI
d())) //finding position
{
i++;
}
for(int j=c-1;j>=i;j--) //shifting
cells
{
eqpList[j+1]=eqpList[j];
}
eqpList[i]=eqp;
c++;
}
return true;
}
return false;
}
public boolean remove(String id)
{
if(id==null)
{
System.out.println("Error - id cannot be
null");
System.exit(0);
}
int f=0,i;
for(i=0;i<c;i++)
{
if(Integer.parseInt(eqpList[i].getId())==Integer.parseInt(id))
{
f=1;
break;
}
}
if(f==1)
{
for(int j=i+1;j<c;j++)
{
eqpList[j-1]=eqpList[j];
}
c--;
return true;
}
return false;
}
public boolean find(String id)
{
if(id==null)
{
System.out.println("Error - id cannot be
null");
System.exit(0);
}
int f=0,i;
for(i=0;i<c;i++)
{
if(Integer.parseInt(eqpList[i].getId())==Integer.parseInt(id))
{
f=1;
break;
}
}
if(f==1)
{
return true;
}
return false;
}
public void display()
{
System.out.println("\nDisplay:");
for(int i=0;i<c;i++)
{
System.out.println("id= "+eqpList[i].getId()+"
desc= "+eqpList[i].getDesc());
}
}
public static void main(String[] args)
{
int choice;
String id,desc;
Scanner sc=new Scanner(System.in);
Equipment myEqp;
IFCSManager myMgr = new IFCSManager();
do
{
System.out.println("1. Add new equipment:");
System.out.println("2. Remove an equipment:");
System.out.println("3. Report an equipment:");
System.out.println("4. Display list:");
System.out.println("5. Exit:");
System.out.print("Enter choice:");
choice=sc.nextInt();
switch(choice)
{
case 1:
for(int i=1;i<=10;i++)
{
System.out.print("ID= ");
id=sc.next();
System.out.print("Description= ");
desc=sc.next();
myEqp= new Equipment(id, desc);
if(myMgr.insert(myEqp))
{
System.out.println("Equipment
added to Lab");
}
}
break;
case 2:
System.out.print("id= ");
id=sc.next();
if(myMgr.remove(id))
System.out.println("Equipment removed
from Lab");
else
System.out.println("Equipment with id
"+id+" cannot be found");
break;
case 3:
System.out.print("id= ");
id=sc.next();
if(myMgr.find(id))
System.out.println("Equipment
available in Lab");
else
System.out.println("Equipment not in
Lab");
break;
case 4:
myMgr.display();
break;
case 5:
break;
default:
System.out.println("Wrong Choice! Try
Again...");
}
}
while(choice!=5);
}
}

Output:

E:\iem\5th Sem\Java\Lab 3>javac IFCSManager.java


E:\iem\5th Sem\Java\Lab 3>java IFCSManager
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:1
ID= 11
Description= a
Equipment added to Lab
ID= 15
Description= b
Equipment added to Lab
ID= 1
Description= g
Equipment added to Lab
ID= 4
Description= o
Equipment added to Lab
ID= 7
Description= h
Equipment added to Lab
ID= 99
Description= io
Equipment added to Lab
ID= 123
Description= ghj
Equipment added to Lab
ID= 5
Description= yu
Equipment added to Lab
ID= 78
Description= oi
Equipment added to Lab
ID= 12
Description= lk
Equipment added to Lab
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:4
Display:
id= 1 desc= g
id= 4 desc= o
id= 5 desc= yu
id= 7 desc= h
id= 11 desc= a
id= 12 desc= lk
id= 15 desc= b
id= 78 desc= oi
id= 99 desc= io
id= 123 desc= ghj
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:2
id= 5
Equipment removed from Lab
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:3
id= 5
Equipment not in Lab
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:2
id= 99
Equipment removed from Lab
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:4
Display:
id= 1 desc= g
id= 4 desc= o
id= 7 desc= h
id= 11 desc= a
id= 12 desc= lk
id= 15 desc= b
id= 78 desc= oi
id= 123 desc= ghj
1. Add new equipment:
2. Remove an equipment:
3. Report an equipment:
4. Display list:
5. Exit:
Enter choice:5
E:\iem\5th Sem\Java\Lab 3>

You might also like