0% found this document useful (0 votes)
64 views23 pages

Assignments Pooja Gaikwad

The document describes 4 exercises involving Java programming concepts. Exercise 1 demonstrates 3 ways to calculate the area of a rectangle using classes - without objects, with objects, and with two classes. Exercise 2 shows 3 ways to model a banking system with deposit and withdrawal methods - without objects, with objects, and with two classes. Exercise 3 involves inheritance with an Employee class and Salary subclass, using constructors and methods. Exercise 4 defines a Shape interface with 2 implementing subclasses - Square and Circle - and demonstrates calculating their areas and perimeters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views23 pages

Assignments Pooja Gaikwad

The document describes 4 exercises involving Java programming concepts. Exercise 1 demonstrates 3 ways to calculate the area of a rectangle using classes - without objects, with objects, and with two classes. Exercise 2 shows 3 ways to model a banking system with deposit and withdrawal methods - without objects, with objects, and with two classes. Exercise 3 involves inheritance with an Employee class and Salary subclass, using constructors and methods. Exercise 4 defines a Shape interface with 2 implementing subclasses - Square and Circle - and demonstrates calculating their areas and perimeters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Name: Pooja Gaikwad

Batch-5

Exercise 1:
java program to illustrate the use of rectangle triangle class which has length and width data
members

Type 1: Without Object


class Rectangle1
{
public static void main(String args[])
{
int width=5;
int height=10;
int Area=width*height;
System.out.println("Area of Rectangle: "+Area);
}
}

Output:
Type 2: Using Object calling method & variables
class Rectangle2
{
int width=5;
int height=10;

void Area_rect()
{
System.out.println("Width is: "+width);
System.out.println("Height is: "+height);

int Area=width*height;
System.out.println("Area of Rectangle: "+Area);
}
public static void main(String args[])
{
Rectangle2 R1=new Rectangle2();
R1.Area_rect();
}
}

Output:
Type 3: Using two classes calling method & variables (Without Arguments)
class Rect
{
int width;
int height;
void Area_rect() //Without Arguments
{
System.out.println("Width is: "+width);
System.out.println("Height is: "+height);
int Area=width*height;
System.out.println("Area of Rectangle: "+Area);
}
}

class Rectangle3
{
public static void main(String args[])
{
Rect R1=new Rect();
Rect R2=new Rect();

R1.width=5;
R1.height=10;
R1.Area_rect();

R2.width=10;
R2.height=6;
R2.Area_rect();
}
}
Output:
Type 3.1: Using two classes calling method & variables(with Arguments)
import java.util.Scanner;
class Rect123
{
int width;
int height;

void GetData(int W,int H) //with Arguments


{
width=W;
height=H;
}
void Area_rect()
{
int Area=width*height;
System.out.println("Area of Rectangle: "+Area);
}
}

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

System.out.println("Enter Width: ");


int width = sc.nextInt();
System.out.println("Enter Height: ");
int height= sc.nextInt();
Rect123 R1=new Rect123();
R1.GetData(width,height);
R1.Area_rect();
}
}

Output:
Exercise 2:
java program to demonstrate the working of a banking system where we deposit and
withdraw amount from our account. creating an account class which has deposit() and
withdraw() methods.
Type 1: Without Object
class Bank1
{
public static void main(String args[])
{
int AccNo=1001;
float Balance=10000;
float Amount=100;

System.out.println("Account Number: "+AccNo);


float Deposite= Balance+Amount;
System.out.println("Deposited !!... Current Balance is: "+Deposite);

float Withdraw = Balance-Amount;


System.out.println("Amount withdrawal !!... Current Balance is: "+Withdraw);
}
}
Output:
Type 2: Using Object calling method & variables
class Bank2
{
int AccNo=1001;
float Balance=10000;
float Amount=100;
void Deposite_Amount(){
System.out.println("Account Number: "+AccNo);
float Deposite= Balance+Amount;
System.out.println("Deposited !!... Current Balance is: "+Deposite);
}
void Withdraw_Amount(){
float Withdraw = Balance-Amount;
System.out.println("Amount withdrawal !!... Current Balance is: "+Withdraw);
}
public static void main(String args[])
{
Bank2 B1=new Bank2();
B1.Deposite_Amount();
B1.Withdraw_Amount();
}
}
Output:
Type 3: Using two classes calling method & variables (Without Arguments)
class Bankdetails
{
int AccNo;
float Balance;
float Amount;
void Deposite_Amount() //Without Arguments
{
System.out.println("Account Number: "+AccNo);
float Deposite= Balance+Amount;
System.out.println("Deposited !!... Current Balance is: "+Deposite);
}

void Withdraw_Amount()
{
float Withdraw = Balance-Amount;
System.out.println("Amount withdrawal !!... Current Balance is: "+Withdraw);
}
}

class Bank3
{
public static void main(String args[])
{
Bankdetails B1=new Bankdetails();
Bankdetails B2=new Bankdetails();

B1.AccNo=1001;
B1.Balance=10000;
B1.Amount=100;
B1.Deposite_Amount();
B1.Withdraw_Amount();

B1.AccNo=1002;
B1.Balance=20000;
B1.Amount=200;
B1.Deposite_Amount();
B1.Withdraw_Amount();
}
}
Output:
Type 3.1: Using two classes calling method & variables (with Arguments)
import java.util.Scanner;
class Bankdetails123
{
int AccNo;
float Balance;
float Amount;

void GetData(int Acc,float Bal,float Amt)//With Arguments


{
AccNo=Acc;
Balance= Bal;
Amount= Amt;
}

void Deposite_Amount()
{
float Deposite= Balance+Amount;
System.out.println("Deposited !!... Current Balance is: "+Deposite);
}

void Withdraw_Amount()
{
float Withdraw = Balance-Amount;
System.out.println("Amount withdrawal !!... Current Balance is: "+Withdraw);
}
}

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

System.out.println("Enter account no: ");


int AccNo = sc.nextInt();
System.out.println("Enter Balance: ");
float Balance = sc.nextFloat();
System.out.println("Enter Amount: ");
float Amount = sc.nextFloat();

Bankdetails123 B1=new Bankdetails123();


B1.GetData(AccNo,Balance,Amount);
B1.Deposite_Amount();
B1.Withdraw_Amount();
}
}
Output:
Exercise 3:
Employee salary Program inheritance in java
Declare a class employee having emp_id and empname as members.Extend class employee
to have a subclass called salary having designation and monthly_salary as members.
Define the following:
– Required constructors and using this keyword
– A method to find and display all details of employee drawing salary more than 20000/- or
otherwise shown error message
– Method main for creating an array for storing these details given as command line
argument and showing usage of above methods .

/* In this program I have implement all the classes, Attributes, Method as per given
statements also use this keyword, Super, Parameterized constructor and Scanner for taking
input from user. */

import java.util.Scanner;
class Employee
{
int emp_id;
String empname;
Employee(int emp_id,String empname) //Parameterized constructor
{
this.emp_id=emp_id; //this reference
this.empname=empname;
}
}
class Salary extends Employee
{
String designation;
float monthly_salary;
Salary(int emp_id, String empname, String designation, float monthly_salary)
//Parameterized constructor
{
super(emp_id,empname); //Super keyword
this.designation=designation;
this.monthly_salary=monthly_salary;
}
void display()
{
if(monthly_salary>20000)
{
System.out.println("Employee ID is: "+emp_id);
System.out.println("Employee Name is: "+empname);
System.out.println("Employee designation is: "+designation);
System.out.println("Employee Salary is: "+monthly_salary);
}
else
{
System.out.println("No Data Found...");
}
}
}
public class Empdata{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter how much employees details you want: ");
int x = sc.nextInt();
for(int i=0;i<x;i++)
{
System.out.println("Enter Employee Id: ");
int emp_id = sc.nextInt();
System.out.println("Enter Employee name: ");
String empname = sc.next();
System.out.println("Enter designation of Employee: ");
String designation = sc.next();
System.out.println("Enter Employee Salary: ");
float monthly_salary = sc.nextFloat();

Salary s= new Salary(emp_id,empname,designation,monthly_salary);


s.display();
}
}
}
Output:
Exercise 4:
create a shape interface having methods area () and perimeter (). create 2 subclasses, triangle
and square that implement the shape interface. create a class sample with main method and
demonstrate the area and perimeters of both the shape classes. you need to handle the values
of length, breath, and radius in respective classes to calculate their area and perimeter.

interface Shape
{
double Area();
double Perimeter();
}
class Square implements Shape
{
double length;

public Square(double length)


{
this.length=length;
}
public double Area()
{
return length*length;
}
public double Perimeter()
{
return 4 * length;
}
}
class Circle implements Shape
{
double radius;
public Circle(double radius)
{
this.radius=radius;
}
public double Area()
{
return Math.PI * radius * radius;
}
public double Perimeter()
{
return 2 * Math.PI * radius;
}
}

public class Sample


{
public static void main(String args[])
{
double length = 4.0;
Square s=new Square(length);
System.out.println("Area of Square is : "+s.Area());
System.out.println("Perimeter of Square is : "+s.Perimeter ());

double radius = 4.0;


Circle c = new Circle(radius);
System.out.println("Area of Circle is : "+c.Area());
System.out.println("Perimeter of Circle is : "+c.Perimeter ());
}
}
Output:
Problem Statement
Display 1 to 10 numbers using For loop, do while loop, while loop by using switch case also
make implementation of If else loop.
import java.util.*;
public class Loops
{
int i=1;
void Whileloop()
{
System.out.println("Output of While_loop : ");
while(i<=10)
{
System.out.println(i);
i++;
}
}
void Dowhileloop()
{
System.out.println("Output of Do_while : ");
do
{
System.out.println(i);
i++;
}while(i<=10);
}
void Forloop()
{
System.out.println("Output of For_loop : ");
for(int i = 1; i <= 10; i++)
{
System.out.println(i);
}
}
void If_else()
{
int number;
System.out.println("Enter Number:");

Scanner sc1 = new Scanner(System.in);


number = sc1.nextInt();
if(number>0)
{
System.out.println("Positive");
}
else if(number<0)
{
System.out.println("Negative");
}
else
{
System.out.println("Zero");
}
}

public static void main(String args[])


{
Loops l1=new Loops();
System.out.println("*********LOOPS*****");
System.out.println("1. While Loop \n2. DoWhile Loop \n3. For Loop \n4. If Else");
System.out.println("Enter your choice:");
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
switch (s)
{
case 1:
l1.Whileloop();
break;
case 2:
l1.Dowhileloop();
break;
case 3:
l1.Forloop();
break;
case 4:
l1.If_else();
break;
default:
System.out.println("Invalid choice......");
break;
}
}
}
Output:

You might also like