ADPJ1
ADPJ1
Assignment - 1
Topic: Inheritance & Interfaces in Java.
_______________________________________________________________________________________
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);
emp2.setData(name, 0, 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
Ans:
class Product
{
int prodId;
double price;
int quantity;
static double totalPrice = 0;
Product(int prodId, double price, int quantity)
{
this.prodId = prodId;
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];
3. Define a class Deposit. The instance variables of the class Deposit are
mentioned below.
Principal Long
Time Integer
Rate Double
TotalAmt Double
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);
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;
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();
}
}
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;
}
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();
}
Ans:
package student;
public class Student {
private String name;
private int roll;
package student;
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();
}
@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;
}
}