0% found this document useful (1 vote)
762 views

Lab 4

The document contains code snippets from 4 Java programs that test different concepts. Test 1 corrects a code sample that was trying to create an object without arguments by adding a no-argument constructor. Test 2 corrects a code sample that was calling a non-existent method 'x' by adding the method. Test 3 corrects a code sample with errors like using a capital letter for a non-static variable, missing imports, and no main method. Test 4 corrects a code sample trying to access a non-static variable from a static context by making the variable static.

Uploaded by

Mohammad Anas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
762 views

Lab 4

The document contains code snippets from 4 Java programs that test different concepts. Test 1 corrects a code sample that was trying to create an object without arguments by adding a no-argument constructor. Test 2 corrects a code sample that was calling a non-existent method 'x' by adding the method. Test 3 corrects a code sample with errors like using a capital letter for a non-static variable, missing imports, and no main method. Test 4 corrects a code sample trying to access a non-static variable from a static context by making the variable static.

Uploaded by

Mohammad Anas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Test 1:

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 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Test2 t2 = new Test2();

t2.x(); }

public void x() {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated


methods, choose Tools | Templates.} }
Activity 1:
Test 3:
Public should be written with a small ‘p’ because when it is written with a capital one, it
then considers ‘Public’ as a class (Since Java is case sensitive). Circle is not a data type like
int, String, double etc. It would throw an error and to rectify this we would have to create a
separate class. Same is the case with getRadius(). This method is used in the program but it
is not defined. There is no main method to execute the program.
Corrected Code:
/**
*
* @author abc
*/

public class Test3 {


public static class Circle {
public Circle() {
}
public int getRadius() {
return 0;
}
}
public static void main(String[] args) {
Circle c = null;
System.out.println("Radius is" + c.getRadius());
c = new Circle();
}
}
Test 4:
Non-static variable cannot be referenced from a static context. To make our code work, we
need to make the variable ‘value’ static as shown in the code below.
Source Code:
/**
*
* @author abc
*/
public class Test4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
C c = new C();
System.out.println(C.value);
}

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;
}

public static void main(String[] args) {


// Create a Rectangle with width 4 and height 40
Rectangle rectangle1 = new Rectangle(4, 40);
// Create a Rectangle with width 3.5 and height 35.9
Rectangle rectangle2 = new Rectangle(3.5, 35.9);
// Display the width, height, area, and perimeter of rectangle1
System.out.println("\n Rectangle 1");
System.out.println("-------------");
System.out.println("Width: " + rectangle1.width);
System.out.println("Height: " + rectangle1.height);
System.out.println("Area: " + rectangle1.getArea());
System.out.println("Perimeter: " + rectangle1.getPerimeter());
// Display the width, height, area, and perimeter of rectangle2
System.out.println("\n Rectangle 2");
System.out.println("-------------");
System.out.println("Width: " + rectangle2.width);
System.out.println("Height: " + rectangle2.height);
System.out.println("Area: " + rectangle2.getArea());
System.out.println("Perimeter: " + rectangle2.getPerimeter());
}
}
Task 2:

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());
}
}

You might also like