Ayushi Jain 19BCE1576: Week-4
Ayushi Jain 19BCE1576: Week-4
Ayushi Jain 19BCE1576: Week-4
AYUSHI JAIN
19BCE1576
Classes and Objects
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;
}
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());
}
}
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());
}
}
package Cse1007;