Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSA Assignment 2

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

Solution of Assignment 2

Question 1.

import java.util.*;
class Person
{
String name;
int age;
public void setData()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name: ");
this.name = sc.next();
System.out.println("Enter the age: ");
this.age = sc.nextInt();
}
public void display()
{
System.out.println(this.name+" "+this.age);
}
}
public class Q1
{
public static void main(String[] args)
{
Person p1 = new Person();
p1.name = "Rohan";
p1.age = 20;
Person p2 = new Person();
p2.setData();
p1.display();
p2.display();
if(p1.age<p2.age)
System.out.println(p1.name+" is younger than "+p2.name);
else
System.out.println(p2.name+" is younger than "+p1.name);
}
}
Question 2.

import java.util.*;
class Complex
{
int real, image;
public void set_data()
{
Scanner sc = new Scanner(System.in);
this.real = sc.nextInt();
this.image = sc.nextInt();
}
public void display()
{
System.out.println(this.real + " + i" + this.image);
}
public Complex add(Complex c1, Complex c2)
{
Complex res = new Complex();
res.real = c1.real + c2.real;
res.image = c1.image +c2.image;
return res;
}
}
public class Q2
{
public static void main(String[] args)
{
Complex c1 = new Complex();
Complex c2 = new Complex();
System.out.print("Enter the real and imaginary value of first Complex
number: ");
c1.set_data();
System.out.print("Enter the real and imaginary value of second Complex
number: ");
c2.set_data();
System.out.print("first Complex number: ");
c1.display();
System.out.print("Second Complex number: ");
c2.display();
Complex res = new Complex();
res = res.add(c1, c2);
System.out.print("Addition is :");
res.display();
}
}

Question 3.

import java.util.*;

class Product
{
int prodID, price, quantity;
static int tot_price;
public Product(int pid, int p, int q)
{
this.prodID = pid;
this.price = p;
this.quantity = q;
tot_price += this.price;
}
public void display()
{
System.out.println(this.prodID+"\t\t"+this.price+"\t\t"+this.quantity);
}
}
public class Q3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Product p[] = new Product[5];
for(int i = 0;i<5;i++)
{
System.out.println("Enter the product id, price, and quantity: ");
int pid = sc.nextInt();
int price = sc.nextInt();
int quantity = sc.nextInt();
p[i] = new Product(pid,price,quantity);
}
System.out.println("All information:\nProduct ID Price");
for(int i = 0;i<5;i++)
p[i].display();
System.out.println("Total price is: "+Product.tot_price);

}
}

Question 4.

import java.util.Scanner;

class Deposit {
long principal;
int time;
double rate, total_amount;
public Deposit()
{
principal = 0;
time = 0;
rate = 0;
}
public Deposit(long p,int t,double d)
{
principal = p;
time = t;
rate = d;
}
public Deposit(long p,int t)
{
principal = p;
time = t;
rate = 5;
}
public Deposit(long p,double d)
{
principal = p;
time = 2;
rate = d;
}
public void calAmt()
{
total_amount = principal + (principal*time*rate)/100;
}
public void display()
{
System.out.println("Your principal is: "+principal);
System.out.println("Loan time: "+time);
System.out.println("Rate of interest: "+rate);
System.out.println("Total payable amount: "+total_amount);
}
}
public class Q4
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Output for first object");
Deposit d = new Deposit(1000,2,3);
d.calAmt();
d.display();
System.out.println("Output for second object");
Deposit d1 = new Deposit(2000,6.0);
d1.calAmt();
d1.display();
}
}

Question 5.

import java.util.*;

class Person
{
String name;
int age;
Person(String name, int age)
{
this.name = name;
this.age = age;
}
}
class Employee extends Person
{
int eID, salary;
public Employee(String name, int age, int eID, int salary)
{
super(name, age);
this.eID = eID;
this.salary = salary;
}
public void empDisplay()
{
System.out.println("*********Employee details***********");
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Employee ID: "+this.eID);
System.out.println("Salary: "+this.salary);
}
}
class Q5
{
public static void main(String[] args)
{
Employee e = new Employee("sankar",20, 1001, 20000);
e.empDisplay();
}
}

Question Q6.

import java.util.*;
abstract class Marks
{
int markICP, markDSA;
double percentage;
public Marks(int markICP, int markDSA)
{
this.markICP = markICP;
this.markDSA = markDSA;
}
public abstract double getPercentage();
}
class CSE extends Marks
{
int algoDesign;
public CSE(int markICP, int markDSA, int algoDesign)
{
super(markICP, markDSA);
this.algoDesign = algoDesign;
}
public double getPercentage()
{
return (this.markICP+this.markDSA+this.algoDesign)/3.0;
}
}
class Non_CSE extends Marks
{
int enggMechanics;
public Non_CSE(int markICP, int markDSA, int enggMechanics)
{
super(markICP, markDSA);
this.enggMechanics = enggMechanics;
}
public double getPercentage()
{
return (this.markICP+this.markDSA+this.enggMechanics)/3.0;
}
}
class Q6
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Object of CSE student");
System.out.println("Enter the marks of ICP, DSA, and Algorithm: ");
int ICP = sc.nextInt();
int DSA = sc.nextInt();
int AD = sc.nextInt();
CSE c = new CSE(ICP, DSA, AD);
System.out.println("Percentage: "+c.getPercentage());
System.out.println("Object of Non-CSE student");
System.out.println("Enter the marks of ICP, DSA, and Mechanics: ");
ICP = sc.nextInt();
DSA = sc.nextInt();
int M = sc.nextInt();
Non_CSE nc = new Non_CSE(ICP, DSA, M);
System.out.println("Percentage: "+nc.getPercentage());
}
}

Question 7.

import java.util.*;
interface DetailInfo
{
void count();
void display();
}
class Person
{
String name;
static int maxCount = 0;
public Person(String name)
{
this.name = name;
}
public void count()
{
maxCount = this.name.length();
}
public void display()
{
System.out.println("Your name: "+this.name);
System.out.println("Number of character present: "+maxCount);
}
}
public class HelloWorld
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a name");
String name = sc.nextLine();
Person p = new Person(name);
p.count();
p.display();
}
}

Question Q8.

Student.java
package p1;

public class Student


{
String name;
int roll;
void input(String n, int r)
{
this.name = n;
this.roll = r;
}
void display()
{
System.out.println("Name: "+this.name);
System.out.println("Roll: "+this.roll);
}
}

Test.java
package p1;
public class Test extends Student
{
public int mark1,mark2;
public void input(String n, int r, int m1,int m2)
{
super.input(n, r);
this.mark1 = m1;
this.mark2 = m2;
}
public void display()
{
super.display();
System.out.println("Marks 1: "+this.mark1);
System.out.println("Marks 2: "+this.mark2);
}
}

Sports.java
package p2;

public interface Sports


{
public char Score1 ='A', Score2 = 'B';
}

Q8.java
import p2.*;
import p1.*;
public class Q8 extends Test implements Sports
{
public int returnTotal()
{
return this.mark1+this.mark2;
}
public static void main(String[] args)
{
Q10 ob = new Q10();
ob.input("Sankar",10,50,60);
ob.display();
System.out.println("Total marks: "+ob.returnTotal());
System.out.println("Score 1: "+Score1);
System.out.println("Score 1: "+Score1);
}
}

Home Assignment
Question 1.

import java.util.*;
class Commission
{
int sales;
public Commission(int sales)
{
this.sales = sales;
}
public double getCommission()
{
if(this.sales<=100)
return this.sales*0.02;
else if(this.sales>100 && this.sales<=5000)
return this.sales*0.05;
else
return this.sales*0.08;
}
}

public class Demo


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sales amount: ");
int sales = sc.nextInt();
Commission C = new Commission(sales);
double com = C.getCommission();
if(com<0)
System.out.println("Invalid Input");
else
System.out.println("Commission: "+com);
}
}

Question 2.

import java.util.*;

class Book
{
String BName, BEdition;
int BPrice;
public Book(String BName, String BEdition, int BPrice)
{
this.BName = BName;
this.BEdition = BEdition;
this.BPrice = BPrice;
}
public void display()
{
System.out.println(this.BName+"\t"+this.BEdition+"\t"+this.BPrice);
}
}
class HQ2
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many book you want to buy: ");
int n = sc.nextInt();
Book b[] = new Book[n];
for(int i = 0;i<n;i++)
{
sc.nextLine();
System.out.println("Enter the book name, edition, and price ");
String name = sc.nextLine();
String edition = sc.nextLine();
int price = sc.nextInt();
b[i] = new Book(name, edition, price);
}
System.out.println("*******Book information*******");
System.out.println("Book name \t edition \t price ");
for(int i = 0;i<n;i++)
{
b[i].display();
}
int index = 0;
for(int i = 1;i<n;i++)
{
if(b[index].BPrice<b[i].BPrice)
index = i;
}
System.out.println("Book details with highest price ");
b[index].display();
}
}

Question 3.

import java.util.*;

class Bank
{
String bankName;
int depositAmount;
static int totalAmount;
public Bank()
{
this.bankName = "";
this.depositAmount = 0;
}
public void setBankName(String bankName)
{
this.bankName = bankName;
}
public void setAmount(int depositAmount)
{
if(depositAmount>=1000)
{
this.depositAmount = depositAmount;
}
else
{
this.depositAmount = 0;
System.out.println("Balance not credited due to low balance");
}
totalAmount += this.depositAmount;
}
public void showData()
{
System.out.println(this.bankNname+"\t"+this.depositAmount);
}
public int bankDetails(Bank[] b)
{
int index = 0;
for(int i = 1;i<b.length;i++)
{
if(b[index].depositAmount>b[i].depositAmount)
index = i;
}
return index;
}
}
class HQ3
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("How many account you want to create: ");
int n = sc.nextInt();
Bank b[] = new Bank[n];
for(int i = 0;i<n;i++)
{
sc.nextLine();
System.out.println("Enter the bank name, and deposit amount
(minimum amount 1000)");
String name = sc.nextLine();
int amount = sc.nextInt();
b[i] = new Bank();
b[i].setBankName(name);
b[i].setAmount(amount);
}
System.out.println("*******Book information*******");
System.out.println("Bank name \t Amount ");
for(int i = 0;i<n;i++)
{
b[i].showData();
}
System.out.println("TotalAmount deposit by the person is
"+Bank.totalAmount);
int index = b[0].bankDetails(b);
System.out.println("Bank details with minimum deposit amount ");
b[index].showData();
}
}

Question Q4.

class Distance
{
double meters, centimeters;
public Distance()
{
this.meters = 0;
this.centimeters = 0;
}
public Distance(double meters, double centimeters)
{
this.meters = meters;
this.centimeters = centimeters;
}
public Distance add(Distance d1, Distance d2)
{
Distance d3 = new Distance();
d3.meters = d1.meters + d2.meters;
d3.centimeters = d1.centimeters + d2.centimeters;
if(d3.centimeters>=100)
{
d3.meters += (int)(d3.centimeters/100);
d3.centimeters = d3.centimeters%100;
}
return d3;
}
public void display()
{
System.out.println(this.meters+" "+this.centimeters);
}
}
public class HQ4
{
public static void main(String[] args)
{
Distance d1 = new Distance(5, 70);
Distance d2 = new Distance(15, 50);
Distance d3 = new Distance();
d3 = d3.add(d1,d2);
d1.display();
d2.display();
d3.display();
}
}

Question 5.

class PointType
{
int xCoordinate,yCoordinate;
public PointType(int x, int y)
{
this.xCoordinate = x;
this.yCoordinate = y;
}
public double returnXcoordinate()
{
return this.xCoordinate;
}
public double returnYcoordinate()
{
return this.yCoordinate;
}
}
class CircleType extends PointType
{
double radius, area, perimeter ;
public CircleType(int x, int y, double r)
{
super(x,y);
this.radius = r;
}
public void operation()
{
this.area = Math.PI*Math.pow(this.radius, 2);
this.perimeter = 2*Math.PI*this.radius;
}
public void display()
{
System.out.println("Circle center x = "+returnXcoordinate()+", and y =
"+returnYcoordinate());
System.out.println("Circle radius: "+this.radius);
System.out.println("Area of Circle: "+this.area);
System.out.println("Perimeter of Circle: "+this.perimeter);
}
}
public class HQ5
{
public static void main(String[] args)
{
CircleType CT = new CircleType(4, 8, 5.5);
CT.operation();
CT.display();
}
}

You might also like