Ayushi Jain 19BCE1576: Week-4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Program B.

Tech Computer Science and Engineering Winter 2020-2021


Course Title Java Programming Embedded Lab CH2020215001086
Course Code CSE1007 Week-4 and L27+L28

AYUSHI JAIN
19BCE1576
Classes and Objects

1. Create a class called Invoice that a hardware store might use to


represent an invoice for an item sold at the store. An Invoice
should include four pieces of information as instance variables‐ a
part number(type String),a part description(type String),a quantity
of the item being purchased (type int) and a price per
item  (double). Your class should have a constructor that initializes
the four instance variables. Provide a set and a get method for
each instance variable. In addition, provide a method named
getInvoice Amount that calculates the invoice amount (i.e.,
multiplies the quantity by the price per item), then returns the
amount as a double value. If the quantity is not positive, it should
be set to 0. If the price per item is not positive, it should be set
to 0.0. Write a test application named InvoiceTest that
demonstrates class Invoice’s capabilities.

package Cse1007;
import java.util.Scanner;
class Invoice
{
String partnumber;
String partdescription;
int quantity;
double price;
Invoice()
{
partnumber = "";
partdescription = "";
quantity = 0;
price = 0.0;
}

String getPartNumber()
{
return partnumber;
}

String getPartDescription()
{
return partdescription;
}

int getQuantity()
{
return quantity;
}

double getPrice()
{
return price;
}

double getInvoice()
{
return (quantity * price);
  }
void setPartNumber(String partnumber)
{
this.partnumber = partnumber;
}

void setPartDescription(String partdescription)


{
this.partdescription = partdescription;
}

void setQuantity(int quantity)


{
this.quantity = quantity;
}
void setPrice(double price)
{
this.price = price;
}
}

class InvoiceTest 
{
public static void main(String args[]) 
{
Scanner sc = new Scanner(System.in);
Invoice invoice = new Invoice();
System.out.println("Part number :");
invoice.setPartNumber(sc.nextLine());
System.out.println("Part description :");
invoice.setPartDescription(sc.nextLine());
System.out.println("Qantity :");
invoice.setQuantity(sc.nextInt());
System.out.println("Price per item :");
invoice.setPrice(sc.nextDouble());
System.out.println("------------------------------------------");
System.out.println("Part number : " + invoice.getPartNumber());
System.out.println("Part description : " + invoice.getPartDescription());
System.out.println("Total Amount : " + invoice.getInvoice());
}
}

2. Create a class called Employee that includes three pieces of


information as instance variables—a first name (typeString), a last
name (typeString) and a monthly salary (double). Your class should
have a constructor that initializes the three instance variables.
Provide a set and a get method for each instance variable. If the
monthly salary is not positive, set it to 0.0. Write a test
application named EmployeeTest that demonstrates class
Employee’s capabilities. Create two Employee objects and display
each object’s yearly salary. Then give each Employee a 10% raise
and display each Employee’s yearly salary again.

package Cse1007;
import java.util.Scanner;
class Employee
{
String firstname;
String lastname;
double salary;
Employee()
{
firstname = "";
lastname = "";
salary = 0.0;
if (salary < 0.0)
salary = 0.0;
}
void setFirstName (String fname)
{
firstname = fname;

String getFirstName ()
{
return firstname;
}
void setLastName (String lname)
{
lastname = lname;
}
String getLastName ()
{
return lastname;

void setMonthlySalary (double salary)
{
this.salary = salary;
}
double getMonthlySalary ()
{
return salary;
}
double getYearlySalary()
{
double yearlySalary = salary * 12;
return yearlySalary;

public double getRaiseSalary()
{
double raise =  salary * 0.1 ;
double raiseSalary = ( salary + raise ) * 12;
return raiseSalary;
}
}

class EmployeeTest 
{
public static void main(String args[]) 
{
Scanner sc = new Scanner(System.in);
Employee employee1 = new Employee();
Employee employee2 = new Employee();
System.out.println("Employee 1 details :");
employee1.setFirstName(sc.nextLine());
employee1.setLastName(sc.nextLine());
employee1.setMonthlySalary(sc.nextDouble());
System.out.println("Employee 2 details :");
employee2.setFirstName(sc.nextLine());
employee2.setLastName(sc.nextLine());
employee2.setMonthlySalary(sc.nextDouble());
System.out.println("------------------------------------------");
System.out.println("Yearly Salary of Employee 1 : " + employee1.getYearlySalary());
System.out.println("Yearly Salary of Employee 2 : " + employee2.getYearlySalary());
System.out.println("Raised Salary of Employee 1 : " + employee1.getRaiseSalary());
System.out.println("Raised Salary of Employee 2 : " + employee2.getRaiseSalary());
}
}
3. Create a class called Date that includes three pieces of information
as instance variables—a month (typeint), a day (typeint) and a
year (typeint). Your class should have a constructor that initializes
the three instance variables and assumes that the values provided
are correct. Provide a set and a get method for each instance
variable. Provide a method displayDate that displays the month,
day and year separated by forward slashes(/). Write a test
application named DateTest that demonstrates classDate’s
capabilities.

package Cse1007;
import java.util.Scanner;
class Date {
int month;
int day;
int year;

Date(){
month=1;
day=1;
year=2000;
}
void setMonth(int Months){
month=Months;
}
  
int getMonth(){
return month;
}
void setDay(int Days){
day=Days;
}
int getDay(){
return day;
}
void setYear(int Years){   
year=Years;
}
int getYear(){
return year;
}
public void displayDate(){
  System.out.printf("%d/%d/%d\n",getMonth(),getDay(),getYear()); 
}
}

public class DateTest {


 
public static void main(String args[]){
Date date=new Date();
Scanner sc=new Scanner(System.in);
System.out.println("Enter Month");
int m=sc.nextInt();
date.setMonth(m);
System.out.println("Enter Day ");
int d=sc.nextInt();
date.setDay(d);
System.out.println("Enter Year");
int y=sc.nextInt();
date.setYear(y);
date.displayDate();
}
}
4. Create class SavingsAccount. Usea static variable annualInterestRate
to store the annual interest rate for all account holders. Each
object of the class contains a private instance variable
savingsBalance indicating the amount the saver currently has
ondeposit. Provide method calculateMonthlyInterest to calculate the
monthly www.oumstudents.tk interest by multiplying the
savingsBalance by annualInterestRate divided by 12 this interest
should be added to savingsBalance. Provide a static method
modifyInterestRate that sets the annualInterestRate to a new value.
Write a program to test class SavingsAccount. Instantiate two
savingsAccount objects, saver1 and saver2, with balances of
$2000.00 and $3000.00, respectively. Set annualInterestRate to 4%,
then calculate the monthly interest and print the new balances for
both savers. Then set the annualInterestRate to 5%, calculate the next
month’s interest and print the new balances for both savers.

package Cse1007;

public class SavingAccount {


static private double annualInterestRate;
private double savingsBalance;
public SavingAccount(double savingsBalance)
{
this.savingsBalance=savingsBalance;
}
public double getSavingBalance()
{
return this.savingsBalance;
}
public static void modifyInterestRate(double newInterestRate)
{
annualInterestRate=newInterestRate;
}
public void calculateMonthlyInterest()
{
double monthlyI; 
monthlyI= (double)(this.savingsBalance*annualInterestRate/12);
    this.savingsBalance+=monthlyI;
}
public static void main(String[] args) {
SavingAccount saver1, saver2;
    saver1 = new SavingAccount(2000.0);
    saver2= new SavingAccount (3000.0);
    int total = 0;
    SavingAccount.modifyInterestRate (0.04);
    saver1.calculateMonthlyInterest();
    saver2.calculateMonthlyInterest();
    System.out.println("This month:\nSaver 1 balance= "+ saver1.getSavingBalance());
    System.out.println("Saver 2 balance= "+ saver2.getSavingBalance());
    SavingAccount.modifyInterestRate(0.05);
    saver1.calculateMonthlyInterest();
    saver2.calculateMonthlyInterest();
    System.out.println("Next month:\nSaver 1 balance= "+ saver1.getSavingBalance());
    System.out.println("Saver 2 balance= "+ saver2.getSavingBalance());
  }
}

You might also like