0% found this document useful (0 votes)
17 views9 pages

ADPJ1

Uploaded by

jenasubhradeep
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)
17 views9 pages

ADPJ1

Uploaded by

jenasubhradeep
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/ 9

Department of Computer Science & Engineering

Faculty of Engineering and Technology (ITER)

Assignment - 1
Topic: Inheritance & Interfaces in Java.
_______________________________________________________________________________________

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.

Ans:
import java.util.Scanner;
class Employee
{
String name;
float salary;
int age;
void setData(String name, float salary, int age)
{
this.name = name;
this.salary = salary;
this.age = age;
}
void displayData()
{
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
System.out.println("Age: " + age);
}
public static void main(String[] args)
{
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.setData("Joseph", 65784.50f, 25);

Scanner sc = new Scanner(System.in);


System.out.print("Enter your name: ");

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

String name = sc.nextLine();

System.out.print("Enter your age: ");


int age = sc.nextInt();

emp2.setData(name, 0, age);

System.out.println("Details of Employee 1:");


emp1.displayData();

System.out.println("Details of Employee 2:");


emp2.displayData();

if (emp1.age > emp2.age)


System.out.println(emp1.name + " is older than "+ emp2.name );
else if (emp1.age < emp2.age)
System.out.println(emp2.name + " is older than "+ emp1.name );
else
System.out.println("Both are of the same age.");

}
}

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.

Ans:
class Product
{
int prodId;
double price;
int quantity;
static double totalPrice = 0;
Product(int prodId, double price, int quantity)
{
this.prodId = prodId;

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

this.price = price;
this.quantity = quantity;
totalPrice += price * quantity;
}
void display()
{
System.out.println("Product ID: " + prodId);
System.out.println("Price: " + price);
System.out.println("Quantity: " + quantity);
}
public static void main(String[] args)
{
Product[] p = new Product[5];

p[0] = new Product(101, 20.0, 2);


p[1] = new Product(102, 30.0, 1);
p[2] = new Product(103, 5.75, 3);
p[3] = new Product(104, 50.0, 1);
p[4] = new Product(105, 15.0, 5);

for (int i = 0; i < 5; i++)


{
System.out.println("Details of Product " + (i + 1) + ":");
p[i].display();
System.out.println();
}

System.out.println("Total amount to pay: " + totalPrice);


}
}

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.

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

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;

Ans:
class Deposit
{
long Principal;
int Time;
double Rate;
double TotalAmt;
Deposit()
{
Principal = 0;
Time = 0;
Rate = 0;
}
Deposit(long p, int t, double r)
{
Principal = p;
Time = t;
Rate = r;
}
Deposit(long p, int t)
{
Principal = p;
Time = t;
Rate = 0;
}
Deposit(long p, double r)
{
Principal = p;
Time = 0;
Rate = r;
}
void calcAmt()
{
TotalAmt = Principal + (Principal * Rate * Time) / 100;
}
void display()
{
System.out.println("Principal: " + Principal);

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

System.out.println("Time: " + Time);


System.out.println("Rate: " + Rate);
System.out.println("Total Amount: " + TotalAmt);
}
public static void main(String[] args)
{
Deposit dep1 = new Deposit(10000, 5, 7.5);
dep1.calcAmt();
dep1.display();
Deposit dep2 = new Deposit(15000, 3);
dep2.Rate = 6.5;
dep2.calcAmt();
dep2.display();
Deposit dep3 = new Deposit();
dep3.calcAmt();
dep3.display();
Deposit dep4 = new Deposit(25000, 3.5);
dep4.Rate = 7.5;
dep4.calcAmt();
dep4.display();
}
}

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.

Ans:
class Employee1 {
String name;
int age;

Employee1(String name, int age) {


this.name = name;
this.age=age;
}
}
class HR extends Employee1 {

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

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("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Employee ID: " + Eid);
System.out.println("Salary: " + salary);
}
public static void main(String[] args) {
HR h = new HR("Dev", 23, 1002, 50000.0);
h.DisplayDetails();
}
}

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.

Ans:
abstract class Marks1 {
int markICP, markDSA;
double percentage;
abstract void getPercentage();
}
class CSE extends Marks1 {
int algoDesign;
CSE(int markICP, int markDSA, int algoDesign) {
this.markICP = markICP;
this.markDSA = markDSA;
this.algoDesign = algoDesign;
getPercentage();
}
@Override
void getPercentage() {
percentage = (markICP + markDSA + algoDesign) / 3.0;

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

}
void display() {
System.out.println("CSE Percentage: " + percentage + "%");
}
}
class NonCSE extends Marks1 {
int enggMechanics;
NonCSE(int markICP, int markDSA, int enggMechanics) {
this.markICP = markICP;
this.markDSA = markDSA;
this.enggMechanics = enggMechanics;
getPercentage();
}
@Override
void getPercentage() {
percentage = (markICP + markDSA + enggMechanics) / 3.0;
}
void display() {
System.out.println("Non-CSE Percentage: " + percentage + "%");
}
}
public class Q5{
public static void main(String[] args)
{
CSE student1 = new CSE(80, 75, 85);
student1.display();
NonCSE student2 = new NonCSE(70, 60, 80);
student2.display();
}

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.

Ans:
package student;
public class Student {
private String name;
private int roll;

public void inputDetails(String name,int roll)


{
this.name=name;
this.roll=roll;

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

public void showDetails() {


System.out.println("Name: " + name);
System.out.println("Roll: " + roll);
}
}

package student;

public class Test extends Student {


int mark1,mark2;

public void inputDetails(int mark1,int mark2)


{
this.mark1=mark1;
this.mark2=mark2;
}

public void showDetails()


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

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.

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

public class Q6{


public static void main(String[] args) {
Student student1 = new Student();
student1.setter("Alice");
student1.display();
System.out.println("Number of characters: " + student1.count());

Name:_____________________ Regd. Number:_____________________


Department of Computer Science & Engineering
Faculty of Engineering and Technology (ITER)

System.out.println("Total number of students: " + Student.getMaxCount());


}
public static class Student implements DetailInfo {
private static int maxcount = 0;
private String name;

public void setter(String name) {


this.name = name;
maxcount++;
}

public String getter() {


return this.name;
}

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

@Override
public int count() {
if (this.name != null) {
return this.name.length();
} else {
return 0;
}
}

public static int getMaxCount() {


return maxcount;
}
}
}

Name:_____________________ Regd. Number:_____________________

You might also like