Lab 4
Lab 4
Constructor Test1 in class test1.Test1 cannot be applied to int. It’s because actual and
formal argument lists differ in length. No argument is required in this case as we are making
an object of test1 (t1).
Corrected Code:
public class Test1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Test1 t1 = new Test1(); } }
Test 2:
There is no method ‘x’ defined.
Corrected Code:
public class Test2 {
/**
*/
t2.x(); }
static class C {
static int value = 2 ;
}
}
Activity 2:
Task 1:
Source Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package rectangle;
/**
*
* @author abc
*/
public class Rectangle {
/**
* @param args the command line arguments
*/
// TODO code application logic here
// variable declarations
private double height;
private double width;
// default constructor
public Rectangle(double wid, double heigh) {
height = heigh;
width = wid;
}
// return area of rectangle
public double getArea() {
return height * width;
}
// returns parameter of a rectangle
public double getPerimeter() {
return 2 * (height + width);
}
// returns width
public double getWidth() {
return width;
}
// returns height
public double getHeight() {
return height;
}
Source Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package employee;
/**
*
* @author abc
*/
public class Employee {
/**
* @param args the command line arguments
*/
// TODO code application logic here
String firstName; // instance variable that stores the first name
String lastName; // instance variable that stores the last name
double monthlySalary; // instance variable
// constructor initializes firstName, lastName and monthlySalary with String and double
supplied as argument
public Employee(String first_name, String last_name, double monthly_salary) {
firstName = first_name; // initialize firstName
lastName = last_name; // initialize lastName
monthlySalary = monthly_salary; // initialize monthlySalary
// if the monthly salary is not positive, set it to 0.0.
if (monthly_salary < 0.0) {
monthlySalary = 0.0;
}
} // end constructor
// method to set the first name
public void setFirstName(String first_name) {
firstName = first_name; // store the first name
} // end method setFirstName
// method to retrieve first name
public String getFirstName() {
return firstName;
} // end method getFirstName
// method to set the last name
public void setLastName(String last_name) {
lastName = last_name; // store the last name
} // end method setLastName
// method to retrieve last name
public String getLastName() {
return lastName;
} // end method getLastName
// method to set the monthly salary
public void setMonthlySalary(double monthly_salary) {
monthlySalary = monthly_salary; // store the monthly salary
} // end method setMonthlySalary
// method to retrieve monthly salary
public double getMonthlySalary() {
return monthlySalary;
} // end method getMonthlySalary
// method to retrieve yearly salary
public double getYearlySalary() {
double yearlySalary = monthlySalary * 12;
return yearlySalary;
} // end method getYearlySalary
// method to retrieve yearly salary after giving 10% raise
public double getRaiseSalary() {
double raise = monthlySalary * 0.1;
double raiseSalary = (monthlySalary + raise) * 12;
return raiseSalary;
} // end method getRaiseSalary
// main method begins program execution
public static void main(String[] args) {
Employee employ_1 = new Employee("Mohammad", "Ahmed", 7500.00);
Employee employ_2 = new Employee("Zain", "Mushtaq", 10000.00);
// display employee's initial yearly salary
System.out.printf("Yearly salary of %s %s: %.2f\n", employ_1.getFirstName(),
employ_1.getLastName(), employ_1.getYearlySalary());
System.out.printf("Yearly salary of %s %s: %.2f\n", employ_2.getFirstName(),
employ_2.getLastName(), employ_2.getYearlySalary());
System.out.println()
// display employee's salary after giving 10% raise
System.out.println("***** Giving 10% raise for each employee *****");
System.out.printf("Yearly salary of %s %s: %.2f\n", employ_1.getFirstName(),
employ_1.getLastName(), employ_1.getRaiseSalary());
System.out.printf("Yearly salary of %s %s: %.2f\n", employ_2.getFirstName(),
employ_2.getLastName(), employ_2.getRaiseSalary());
} // end method main
}
Task 3:
Source Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package date;
/**
*
* @author abc
*/
public class Date {
/**
* @param args the command line arguments
*/
int month;
int day;
int year;
// constructor
public Date(int monthValue, int dayValue, int yearValue) {
month = monthValue;
day = dayValue;
year = yearValue;
} // end three-argument constructor
// set the month
public void setMonth(int monthValue) {
month = monthValue;
} // end method setMonth
public int getMonth() {
return month;
} // return month
// set the day
public void setDay(int dayValue) {
day = dayValue;
} // end method setDay
public int getDay() {
return day;
} // return day
// set the year
public void setYear(int yearValue) {
year = yearValue;
} // end method setYear
public int getYear() {
return year;
} // return year
public static void main(String[] args) {
// TODO code application logic here
Date date = new Date(2,15,20 );
System.out.println(date.getMonth() + "/" + date.getDay() + "/" + date.getYear());
}
}