Exercise 6

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Date:05/11/2023

Ex. No:6

SSN College of Engineering


Department of Computer Science and Engineering
II Year CSE (III Semester)
Academic Year 2023-24
UCS2313 – Object Oriented Programming Lab

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

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

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(){

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

return this.fueltype;
}
public int energyConsEnquiry(){
return 15;
}
}

//CLASS MACHINESDEMO OUTSIDE MACHINES PACKAGE

import machines.*;

public class MachinesDemo{

public static void main(String args[]){

HomeAppliance f=new HomeAppliance("Fan","E");


System.out.println("Name:"+f.name+" "+"FuelType:"+f.-
fuelEnquiry()+" "+"EnergyCOnsumption:"+f.energyConsEnquiry());

FactoryEquipment im=new FactoryEquipment("3-Phase In-


duction Motor","E");
System.out.println("Name:"+im.name+"
"+"FuelType:"+im.fuelEnquiry()+" "+"EnergyCOnsumption:"+im.energy-
ConsEnquiry());

Vehicle b=new Vehicle("Bike","P");


System.out.println("Name:"+b.name+" "+"FuelType:"+b.-
fuelEnquiry()+" "+"EnergyCOnsumption:"+b.energyConsEnquiry());

}
}

Output

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

Q.No 2

Java Code
//INSIDE MATHS.OPERATIONS

//CLASS ADDITION
package maths.operations;

public class Addition{


public int a;
public int b;
public Addition(int a,int b){
this.a=a;
this.b=b;
}
public int add(){
return a+b;
}
}

//CLASS SUBTRACTION

package maths.operations;

public class Subtraction{


public int a;
public int b;
public Subtraction(int a,int b){
this.a=a;
this.b=b;
}
public int sub(){
return a-b;
}
}

//CLASS MULTIPLICATION
package maths.operations;

public class Multiplication{


public int a;
public int b;
public Multiplication(int a,int b){
this.a=a;
this.b=b;

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

}
public int mul(){
return a*b;
}
}

//CLASS BASIC OPERATION OUTSIDE MATHS.OPERATIONS


import maths.operations.*;
import java.util.*;

public class BasicOperation{


public static void main(String args[]){
Scanner scan=new Scanner(System.in);
System.out.println(".....BASIC CALCULATOR.....");
System.out.println("1.ADD 2.SUB 3.MUL 4.MATOPERA-
TIONS");
System.out.println("CHoosE:");
int op=scan.nextInt();
System.out.println("Enter A:");
int A=scan.nextInt();
System.out.println("Enter B:");
int B=scan.nextInt();

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.print("Enter the size of the


matrices (n x n): ");
int n = scanner.nextInt();

int[][] matrixA = new int[n][n];


int[][] matrixB = new int[n][n];

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

System.out.println("Enter the elements of


matrix A:");
m.inputMatrix(matrixA, scanner);

System.out.println("Enter the elements of


matrix B:");
m.inputMatrix(matrixB, scanner);

System.out.println("Matrix A:");
m.printMatrix(matrixA);

System.out.println("Matrix B:");
m.printMatrix(matrixB);

int[][] sum = m.addMatrices(matrixA, mat-


rixB);
int[][] difference =
m.subtractMatrices(matrixA, matrixB);
int[][] product = m.multiplyMatrices(mat-
rixA, 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

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

Department of Computer Science and Engineering


Date:05/11/2023
Ex. No:6

Learning Outcomes

Through this exercise,


I learned how to create user defined packages

I learned how to access it outside the package

I learned creating subpackages and also their uses.

Department of Computer Science and Engineering

You might also like