CER3C2 Lab Assignment 05
CER3C2 Lab Assignment 05
import java.util.Scanner;
class Commission{
private int sales;
Commission(int data){
sales = data;
}
public float commission(){
return .1f * sales;
}
}
class demo{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sales: ");
int sales = sc.nextInt();
if(sales<0){
System.out.println("Invalid Input");
}
else{
Commission c = new Commission(sales);
float commission = c.commission();
System.out.println("Commission = "+commission);
}
}}
INPUT/OUTPUT
Enter the sales:
450000
Commission = 45000.0
SOLUTION 02
import java.util.Scanner;
class Point{
//Coordinates
int x;
int y;
Point(int a , int b){
x = a;
y = b;
}
}
class Circle{
int x;
int y;
Point p = new Point(4,5);
Circle(Point p){ //Copy Constructor
x = p.x;
y = p.y;
}
INPUT/OUTPUT
Enter the radius
5
The Coordinates of the circle are:
x=2
y=4
Area = 78.53981633974483
SOLUTION 03
class Time{
private int sec;
private int min;
private int hour;
public Time(int sec1 , int min1 , int hour1){
sec = sec1;
min = min1;
hour = hour1;}
public void add(Time t1){
int seconds = sec + t1.sec;
int minutes = min + t1.min;
int hours = hour + t1.hour;
System.out.println("Seconds = "+seconds+"\nMinutes = "+ minutes+"\nHours =
"+hours);
}
}
public class demo {
public static void main(String[] args) {
Time t = new Time(2,4,6);
Time t1 = new Time(1,3,5);
t.add(t1);
}}
INPUT/OUTPUT
Seconds = 3
Mintes = 7
Hours = 11
SOLUTION 04
class Complex{
private int real;
private int imaginary;
INPUT/OUTPUT
Sum is : 6 + 8i
SOLUTION 05
class Reverse {
int num;
Reverse(int a){
num = a;
}
INPUT/OUTPUT
Reverse is = 341
SOLUTION 06
import java.util.Scanner;
void Input() {
System.out.println("Enter the name of the student");
name = S.nextLine();
void Display() {
System.out.println("Name : " + name);
System.out.println("Roll Number : " + roll_no);
System.out.println("Marks in Sub1 and Sub2 respectively : " + sub1 + " " + sub2);
System.out.println("Total marks are : " + (sub1 + sub2));
System.out.println("Percentage : " + (sub1 + sub2) / 2);
}
}
class Driver {
public static void main(String[] args) {
Student S1 = new Student();
S1.Input();
S1.Display();
}
}
INPUT/OUTPUT
Enter the name of the student
Divyansh
Enter the roll number and marks of Sub1 and Sub2
30 84 68
Name : Divyansh
Roll Number : 30
Marks in Sub1 and Sub2 respectively : 84 68
Total marks are : 152
Percentage : 76
SOLUTION 07
import java.util.Scanner;
void Input() {
System.out.println("Enter the Employee ID");
emp_id = S.nextInt();
System.out.println("Enter the Employee Name");
emp_name = S.next();
System.out.println("Enter the Basic Salary");
basic_salary = S.nextInt();
}
void Display() {
gross_salary = basic_salary + 0.05 * basic_salary;
System.out.println("The gross salary is : " + gross_salary);
}
}
class Driver {
public static void main(String[] args) {
Employee E1 = new Employee();
E1.Input();
E1.Display();
}
}
INPUT/OUTPUT
Enter the Employee ID
20
Enter the Employee Name
Anando
Enter the Basic Salary
50000
The gross salary is : 52500.0
SOLUTION 08
import java.util.Scanner;
class Item {
int code, price;
Item(int c, int p) {
code = c;
price = p;
}
INPUT/OUTPUT
Enter the code for Item 1
101
Enter the price for Item 1
34
Enter the code for Item 2
102
Enter the price for Item 2
45
Enter the code for Item 3
103
Enter the price for Item 3
99
Enter the code for Item 4
104
Enter the price for Item 4
151
Enter the code for Item 5
105
Enter the price for Item 5
121
Item Code Item Price
--------- ----------
101 34
102 45
103 99
104 151
105 121
The total price is : 450
SOLUTION 09
import java.util.Scanner;
class Tender {
String company_name;
int cost;
INPUT/OUTPUT
Enter the name of company 1
Nightloft
Enter the cost for company 1
30000
Enter the name of company 2
SilverSure
Enter the cost for company 2
20000
Enter the name of company 3
HellBoy
Enter the cost for company 3
15000
Enter the name of company 4
Ryuga
Enter the cost for company 4
50000
Enter the name of company 5
AweMonk
Enter the cost for company 5
70000
The minimum cost is for the company : HellBoy
SOLUTION 10
import java.util.Scanner;
class Circle {
private double radius;
private String colour;
Circle() {
radius = 1.0;
colour = "red";
}
Circle(double r) {
radius = r;
colour = "red";
}
INPUT/OUTPUT
C1 has radius : 1.0
Area of C1 : 3.14
C2 has radius : 3.0 Area of C2 : 28.259999999999998
Area of C2 : 28.259999999999998
SOLUTION 11
import java.util.Date;
class Account {
private int ID;
private double Balance;
static private double annualInterestRate;
private Date dateCreated;
Account(){
ID=0;
Balance=0;
annualInterestRate=0;
dateCreated=new Date();
}
Account(int id,double bal){
ID=id;
Balance=bal;
annualInterestRate=0;
dateCreated=new Date();
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public double getBalance() {
return Balance;
}
public void setBalance(double balance) {
Balance = balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public static void setAnnualInterestRate(double annualInterestRate) {
Account.annualInterestRate = annualInterestRate;
}
public Date getDateCreated() {
return dateCreated;
}
public static double getMonthlyInterestRate(){
return ((getAnnualInterestRate()/(100*12))*100);
}
public double getMonthlyInterest(){
return (Balance*(getMonthlyInterestRate()/100));
}
public void withdraw(int amount){
Balance-=amount;
System.out.println("withdrawed "+amount);
System.out.println("balance= " +getBalance());
}
public void deposit(int amount){
Balance+=amount;
System.out.println("deposited "+amount);
System.out.println("balance= " +getBalance());
}
}
public class demo{
public static void main(String[] args) {
Account firstaccount=new Account();
Account Secondaccount=new Account(101, 1000);
System.out.println("first account balance "+firstaccount.getBalance());
System.out.println("second account balance "+Secondaccount.getBalance());
firstaccount.deposit(100);
Secondaccount.withdraw(100);
}
}
INPUT/OUTPUT
INPUT/OUTPUT
}
}
INPUT/OUTPUT
return length*width;
}
@Override
public double calculatePerimeter() {
return 2*(length+width);
}
}
class Triangle extends Shape{
private double s1;
private double s2;
private double s3;
public Triangle(double s1, double s2, double s3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
public Triangle(){
s1=s2=s3=0;
}
@Override
public double calculateArea() {
double s=calculatePerimeter()/2;
return Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
}
@Override
public double calculatePerimeter() {
return (s1+s2+s3);
}
}
public class abstract demo {
public static void main(String[] args) {
Rectangle rectangle=new Rectangle(5,10);
System.out.println("Area of Rectangle "+rectangle.calculateArea());
System.out.println("Perimeter of Rectangle "+rectangle.calculatePerimeter());
Triangle triangle=new Triangle(2,2,2);
System.out.println("Area of Triangle "+triangle.calculateArea());
System.out.println("Perimeter of Triangle "+triangle.calculatePerimeter());
}
}
INPUT/OUTPUT
Area of Rectangle 50.0
Perimeter of Rectangle 30.0
Area of Triangle 1.7320508075688772
Perimeter of Triangle 6.
SOLUTION 16
interface vehicle{
void start();
void stop();
}
class Car implements vehicle{
@Override
public void start() {
System.out.println("Car has started");
@Override
public void stop() {
System.out.println("Car has stopped");
}
class MotorCycle implements vehicle{
@Override
public void start() {
System.out.println("MotorCycle has started ");
@Override
public void stop() {
System.out.println("MotorCycle has stopped");
}
public class interface demo {
public static void main(String[] args) {
Car c=new Car();
c.start();
c.stop();
MotorCycle m=new MotorCycle();
m.start();
m.stop();
}
}
INPUT/OUTPUT
class ThreeDObject{
public double wholeSurfaceArea(){
return 0;
}
public double volume(){
return 0;
}
}
class Box extends ThreeDObject{
private double length;
private double width;
private double height;
}
class Cube extends ThreeDObject{
private double sideLength;
public Cube(double sideLength) {
this.sideLength = sideLength;
}
@Override
public double volume() {
return sideLength*sideLength*sideLength;
}
@Override
public double wholeSurfaceArea() {
return 6*sideLength*sideLength;
}
}
class Cylinder extends ThreeDObject{
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
@Override
public double volume() {
return 3.14*radius*radius*height;
}
@Override
public double wholeSurfaceArea() {
return 2*3.14*radius*(height+radius);
}
}
class Cone extends ThreeDObject{
private double height;
private double radius;
public Cone(double height, double radius) {
this.height = height;
this.radius = radius;
}
@Override
public double volume() {
return (3.14*radius*radius*height)/3;
}
@Override
public double wholeSurfaceArea() {
double slant=Math.sqrt(radius*radius+height*height);
return 3.14*radius*(radius+slant);
}
}
public class threeDobjectdemo {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("Enter length, width and height of box");
double length=s.nextDouble();
double width=s.nextDouble();
double height=s.nextDouble();
Box b=new Box(length, width, height);
System.out.println("volume of box is "+b.volume());
System.out.println("whole surface area of box is "+b.wholeSurfaceArea());
System.out.println("Enter Side Length of Cube");
length=s.nextDouble();
Cube c=new Cube(length);
System.out.println("Volume of Cube is "+c.volume());
System.out.println("Whole surface area of cube is "+c.wholeSurfaceArea());
System.out.println("Enter height and radius of cylinder");
height=s.nextDouble();
double radius=s.nextDouble();
Cylinder cylinder=new Cylinder(radius,height );
System.out.println("Volume of Cylinder is "+cylinder.volume());
System.out.println("Whole Surface Area of Cylinder is "+
cylinder.wholeSurfaceArea());
System.out.println("Enter height and radius of cone ");
height=s.nextDouble();
radius=s.nextDouble();
Cone cone=new Cone(height, radius);
System.out.println("Volume of cone is "+cone.volume());
System.out.println("Whole Surface area of cone is "+cone.wholeSurfaceArea());
}
}
INPUT/OUTPUT
public Bus(String regnNumber, int speed, String color, String ownerName, int
routeNumber) {
super(regnNumber, speed, color, ownerName);
this.routeNumber = routeNumber;
}
@Override
public void showData() {
System.out.println("Registration Number: "+ regnNumber);
System.out.println("Speed: "+speed);
System.out.println("Color: "+color);
System.out.println("Owner Name: "+OwnerName );
System.out.println("Route Number: "+ routeNumber);
super.showData();
}
}
class Car extends Vehicle{
private String manufacturerName;
public Car(String regnNumber, int speed, String color, String ownerName, String
manufacturerName) {
super(regnNumber, speed, color, ownerName);
this.manufacturerName = manufacturerName;
}
@Override
public void showData() {
System.out.println("Registration Number: "+ regnNumber);
System.out.println("Speed: "+speed);
System.out.println("Color: "+color);
System.out.println("Owner Name: "+OwnerName );
System.out.println("Manufacturer Name: "+ manufacturerName);
super.showData();
}
}
public class vehicledemo {
public static void main(String[] args) {
Car car=new Car("1120", 100, "Black","Atharva Aher", "Toyota");
car.showData();
Bus bus=new Bus("1130", 50, "Red", "Anando Sharma", 10);
bus.showData();
}
}
INPUT/OUTPUT
System.out.println(descriptor);
System.out.println(date);
System.out.println(current_value);
System.out.println(num_shares);
System.out.println(share_price);
System.out.println(asset);
}
}
abstract class Bond extends Asset{
System.out.println(interest_rate);
System.out.println(asset);
System.out.println(interest_rate);
System.out.println(asset);
class MIN{
Savings.displayDetails();
Bond.displayDetails();
INPUT/OUTPUT
21
4500
543
5
5678
3
344
SOLUTION 20
import java.util.Scanner;
interface Department{
String DeptName="Computer Science";
String DeptHead="Atharva Aher";
void printAttributes();
}
class hostel {
protected String hostelName;
protected int hostelLocation;
private int numberOfRooms;
}
class Student extends hostel implements Department{
private String studentName;
private int regdNo;
private String electiveSubject;
private int avgMarks;
}
public class Driver {
static Student[] arr=new Student[100];
static int ptr=0;
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int choice;
do{
System.out.println("1.Admit new student");
System.out.println("2.Migrate a student");
System.out.println("3.Display details of a student");
System.out.println("4.Exit");
choice=s.nextInt();
switch(choice){
case 1:
String name,elective;
int regdNo,marks;
System.out.println("Enter student name");
name=s.nextLine();
s.next();
System.out.println("Enter elective subject");
elective=s.nextLine();
s.next();
System.out.println("Enter registration number");
regdNo=s.nextInt();
}
break;
case 4:
System.out.println("Exiting");
break;
default:
System.out.println("invalid");
break;
}while(choice!=4);
}
}
INPUT/OUTPUT