Professional Documents
Culture Documents
Exercise 6
Exercise 6
Exercise 6
Ex. No:6
Exercise 6: Packages
Objective:
● To test the creation and usage of packages in Java
Learning Outcome:
● Create a user defined package and access it outside the package
● Create subpackages and their usage
Best Practices:
● Class Diagrams
● Proper naming conventions
● Comments at proper places
● Prompt messages during reading input and displaying output
● Modularity
● Coverage of test cases
Questions:
1. Create a package named machines with the following three java classes.
a. Class HomeAppliance
b. Class Vehicle
c. Class FactoryEquipment
The classes should contain methods to return the fuelType (electricity, petrol, or diesel) and
energyConsumption (in units per hour in case of electricity, and km per litre in case of
petrol/diesel). Use this package to develop a Java program to print the type of fuel and energy
consumed. This program should be in a Java file outside the package machines.
2. Write a program to create a package maths.operations having three classes: Addition, Multi-
plication and Subtraction. Define suitable methods in each class to perform basic operations.
Inside the package maths, write a Java program to take as input two numbers, and perform
the specified operations by creating classes as applicable.
Aim
The aim of this exercise is to familiarize with the creation
and usage of packages in java.
Q.No 1
Java Code
//INSIDE MACHINES PACKAGE
//CLASS HOMEAPPLIANCE
package machines;
public class HomeAppliance{
public String name;
String fueltype;
public HomeAppliance(String name,String fueltype){
this.name=name;
this.fueltype=fueltype;
}
public String fuelEnquiry(){
return this.fueltype;
}
public int energyConsEnquiry(){
return 2;
}
}
//CLASS VEHICLE
package machines;
public class Vehicle{
public String name;
String fueltype;
public Vehicle(String name,String fueltype){
this.name=name;
this.fueltype=fueltype;
}
public String fuelEnquiry(){
return this.fueltype;
}
public int energyConsEnquiry(){
return 45;
}
}
//CLASS FACTORYEQUIPMENTS
package machines;
public class FactoryEquipment{
public String name;
String fueltype;
public FactoryEquipment(String name,String fueltype){
this.name=name;
this.fueltype=fueltype;
}
public String fuelEnquiry(){
return this.fueltype;
}
public int energyConsEnquiry(){
return 15;
}
}
import machines.*;
}
}
Output
Q.No 2
Java Code
//INSIDE MATHS.OPERATIONS
//CLASS ADDITION
package maths.operations;
//CLASS SUBTRACTION
package maths.operations;
//CLASS MULTIPLICATION
package maths.operations;
}
public int mul(){
return a*b;
}
}
switch(op){
case 1:
Addition obj=new Addition(A,B);
System.out.println(obj.add());
break;
case 2:
Subtraction obj1=new Subtraction(A,B);
System.out.println(obj1.sub());
break;
case 3:
Multiplication obj2=new
Multiplication(A,B);
System.out.println(obj2.mul());
break;
case 4:
Matrix m=new Matrix();
Scanner scanner = new Scanner(System.in);
System.out.println("Matrix A:");
m.printMatrix(matrixA);
System.out.println("Matrix B:");
m.printMatrix(matrixB);
System.out.println("A + B:");
m.printMatrix(sum);
System.out.println("A - B:");
m.printMatrix(difference);
System.out.println("A x B:");
m.printMatrix(product);
scanner.close();
break;
default:
System.out.println("<<INVALID
OPERATION>>");
break;
}
}
}
Output
Learning Outcomes