Assignment 1 Debi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Assignment - I

Topic: Inheritance & Interfaces in Java


Name: Debi Prasad Rath
Registration Number: 2251018009
Section: I

1. Define a class Employee with two instance variables:


● name, salary and age
and two member methods:
● setData(): set the details of the person.
● displayData(): display the details of the person.
Now, create two objects of class Employee and initialize one object value
directly (by using the dot(.) operator name: “Joseph”, salary: 65784.50 and
age: 25 ). Accept your name and age through the keyboard and set them to
another object using the setData() method.
Now display both the member variables using the displayData() method.
Also, check who is older.

CODE:
package Assignment1;
import java.util.*;
class Employee {
String name;
double salary;
int age;

public void setData(String name, double salary, int age){


this.name=name;
this.salary=salary;
this.age=age;
}

public void getData(){


System.out.println("Name: "+name);
System.out.println("Salary: "+salary);
System.out.println("Age: "+age);
}
}

public class Q1{


public static void main(String[] args) {
Scanner in=new Scanner(System.in);

Employee employee1=new Employee();


Employee employee2=new Employee();

employee1.name="Joseph";
employee1.salary=65784.50;
employee1.age=25;

System.out.println("Enter the Employee name: ");


String name=in.nextLine();
System.out.println("Enter the Employee age: ");
int age=in.nextInt();
double salary=70000.10;
employee2.setData(name,salary,age);

System.out.println("\nEmployee1 details:");
employee1.getData();
System.out.println("\nEmployee2 details:");
employee2.getData();

if(employee1.age < employee2.age){


System.out.println("\nEmployee2 is older");
}else if(employee1.age > employee2.age){
System.out.println("\nEmployee1 is older");
}else{
System.out.println("\nBoth have same age");
}
}
}
OUTPUT:

2. In a supermarket each product has minimum details like prodId, price,


quantity that is used during the biling process. Keeping this in mind
prepare a class named as Product having the member variables
● prodId, price, quantity
● a static variable totalPrice
Initialize the value of the product through a parameterized constructor. It
consists of a display() method to display the value of instance variables. A
person went to the market and purchased 5 different products. Using the
above mentioned class, display the details of products that the person has
purchased. Also, determine how much total amount the person will pay for
the purchase of 5 products.

CODE:
package Assignment1;
class Product{
String prodId;
int quantity;
double price;
static double totalPrice;

Product(String prodId, int quantity, double price){


this.prodId=prodId;
this.price=price;
this.quantity=quantity;
totalPrice+=quantity*price;
}

void display(){

System.out.println("Product ID: "+prodId+


"\n Quantity: "+quantity+
"\n Price: "+price);
System.out.println("TOTAL PRICE FOR PRODUCT "+prodId+": "+quantity*price
+"\n");
}
}
public class Q2 {
public static void main(String[] args) {
Product p1=new Product("001",15, 4);
Product p2=new Product("002",10, 6);
Product p3=new Product("003",25, 2);
Product p4=new Product("004",5, 10);
Product p5=new Product("005",20, 3);

System.out.println("PRODUCT DETAILS \n");


p1.display();
p2.display();
p3.display();
p4.display();
p5.display();
System.out.println("Total Price Customer will pay: "+Product.totalPrice);
}
}
OUTPUT:

3. Define a class Deposit. The instance variables of the class Deposit are
mentioned below.
Instance Variable Data Type
Principal Long
Time Integer
Rate Double
TotalAmt Double
Initialize the instance variables Principal, Time, rate through constructors.
Constructors are overloaded with the following prototypes.
Constructor1: Deposit ( )
Constructor2: Deposit (long, int, double)
Constructor3: Deposit (long, int)
Constructor4: Deposit (long, double)
Apart from constructors, the other instance methods are (i) display ( ): to
display the value of instance variables, (ii) calcAmt( ): to calculate the total
amount. totalAmt = Principal + (Principal * rate * Time)/100;
CODE:
package Assignment1;

class Deposit{
long principal;
int time;
double rate, totalAmt;

Deposit(){ //constructor1

Deposit(long p, int t, double r){ //constructor2


principal=p;
time=t;
rate=r;
}

Deposit(long p, int t){ //constructor3


principal=p;
time=t;
}

Deposit(long p, double r){ //constructor4


principal=p;
rate=r;
}

void display(){
System.out.println("Principal amount: "+principal+ "\n"
+"Rate of Interest: "+rate+ "\n"
+"Time period: "+time+ "\n"
+"Total Amount: "+totalAmt);
}

void calcAmt(){
totalAmt=principal + (principal * rate * time)/100;
}
}
public class Q3 {
public static void main(String[] args) {
Deposit p1=new Deposit();
Deposit p2=new Deposit(1000,5,20);
Deposit p3=new Deposit(1000,5);
Deposit p4=new Deposit(1000, 20);
p2.calcAmt();
p2.display();
}
}

OUTPUT:
4. Define a base class Employee with instance variable name, age. The
instance variables are initialized through constructors. The prototype of the
constructor is as below.
Employee (string, int)
Define a derived class HR with instance variables Eid, salary. The instance
variables are initialized through constructors. The prototype of the
constructor is as below.
HR (string, int, int, double).
Another instance method of the HR class is DisplayDetails() to display the
information of HR details.
CODE:
package Assignment1;

class Employee1{
String name;
int age;

Employee1(String name, int age){


this.name=name;
this.age=age;
}
}

class HR extends Employee1{


int eID;
double salary;

HR(String name, int age, int eID, double salary){


super(name,age);
this.eID=eID;
this.salary=salary;
}

void displayDetails(){
System.out.println("EMPLOYEE DETAILS");
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("ID: "+eID);
System.out.println("Salary: "+salary);
}
}
public class Q4 {
public static void main(String[] args) {
HR e=new HR("Chandan",25,2141013059,70000);
e.displayDetails();
}
}
OUTPUT:

5. Create an abstract class Marks with three instance variables


(markICP, markDSA, and percentage) and an abstract method getPercentage().
Create two classes: CSE with instance variable algoDesign, and NonCSE
with instance variable enggMechanics. Both classes inherit the abstract
class Marks and override the abstract method getPercentage(). The
constructor of class CSE takes the marks in three subjects (markICP,
markDSA, and algoDesign) as its parameters, and the constructor of class
NonCSE takes the marks in three subjects (markICP, markDSA, and
enggMechanics) as its parameters. Create an object for each of the two
classes and print the percentage of marks for both students.
CODE:
package Assignment1;

abstract class Marks{


float markICP, markDSA;
float percentage;

abstract float getPercentage();


}

class CSE extends Marks{


float algoDesign;
CSE(float markICP, float markDSA, float algoDesign){
this.markICP=markICP;
this.markDSA=markDSA;
this.algoDesign=algoDesign;
}

float getPercentage(){
return percentage=(markICP+markDSA+algoDesign)/3;
}
}

class nonCSE extends Marks{


float enggMechanics;
nonCSE(float markICP, float markDSA, float enggMechanics){
this.markICP=markICP;
this.markDSA=markDSA;
this.enggMechanics=enggMechanics;
}

float getPercentage(){
return percentage=(markICP+markDSA+enggMechanics)/3;
}
}
public class Q5 {
public static void main(String[] args) {
CSE s1=new CSE(90,85,75);
nonCSE s2=new nonCSE(80,65,95);
System.out.println("The percentage of CSE student: "+s1.getPercentage());
System.out.println("The percentage of NonCSE student: "+s2.getPercentage());
}
}

OUTPUT:

6. Define an interface DetailInfo to declare methods display( ) & count( ).


Another class Student contains a static data member maxcount, instance
member name & method setter(String name) to assign the values to the
instance variable and getter( ) to display the name of a student, count the
no. of characters present in the name of the student.
CODE:
package Assignment1;

interface DetailInfo{
void display();
int count();
}

class Student implements DetailInfo{


static int maxCount=0;
String name;

public void setter(String name){


this.name=name;
}

public String getter(){


return name;
}

@Override
public void display(){
System.out.println("Student name: "+name);
}

@Override
public int count(){
maxCount= name.length();
return maxCount;
}
}

public class Q6 {
public static void main(String[] args) {
Student s1=new Student();
s1.setter("Chandan");
s1.display();
int maxCount = s1.count();
System.out.println("MaxCount: " + maxCount);
}
}

OUTPUT:

7. Design a package that contains two classes: Student & Test. The Student
class has data members as name, roll and instance methods inputDetails()
& showDetails(). Similarly the Test class has data members as mark1,
mark2 and instance methods inputDetails(), showDetails(), Student is
extended by Test. Another package carry interface Sports with 2 attributes
score1, score2. Find grand total mark & score in another class.
CODE:

Student.java

package A1Q7;
import java.util.*;

public class Student{


String name;
int roll;

void inputDetails(){
Scanner in=new Scanner(System.in);
System.out.println("Enter the student name: ");
name=in.nextLine();
System.out.println("Enter the roll number: ");
roll=in.nextInt();
}

void showDetails(){
System.out.println("Student name: "+name);
System.out.println("Roll No: "+roll);
}
}
Test.java

package A1Q7;
import java.util.Scanner;

public class Test extends Student{


int mark1, mark2;

void inputDetails(){
super.inputDetails();
Scanner in=new Scanner(System.in);
System.out.println("Enter the mark1: ");
mark1=in.nextInt();
System.out.println("Enter the mark2: ");
mark2=in.nextInt();
}

void showDetails(){
super.showDetails();
System.out.println("Mark1 is: "+mark1);
System.out.println("Mark2 is: "+mark2);
}
}

Sports.java

package A1Q7;

interface Sports {
int score1 = 60;
int score2 = 40;
}

Calculate.java

package A1Q7;

public class Calculate extends Test implements Sports {


void calculate(){
int marks=mark1+mark2;
int score=score1+score2;
System.out.println("Total marks: "+marks);
System.out.println("Total Score: "+score);
}
}

Main.java

package A1Q7;
public class Main {
public static void main(String[] args) {
Calculate s1=new Calculate();
s1.inputDetails();
s1.calculate();
}
}
OUTPUT:

You might also like