Task 2: Make a Rectangle class that has color,
width and height attribute. Color is of String type, while other two are int
type attribute. All the attributes should be private and exposed via
setter/getter methods. Define a method inside Rectangle class: int calculateArea()
that returns area of Rectangle. Define another method in Rectangle class: int
calculatePerimeter() that returns perimeter of rectangle. Make a
RectangleTest class, in main method, instantiate a rectangle object, sets its
width and height, and prints its area and perimeter.
public class Rectangle
{
private String color;
private double width;
private double height;
public void setcolor(String color)
{
this.color = color;
}
public String getcolor()
{
return color;
}
public void setheight(double height)
{
this.height = height;
}
public double getheight()
{
return height;
}
public void setwidth(double width)
{
this.width = width;
}
public double getwidth()
{
return width;
}
public double calculateArea(double width , double height)
{
double a = height*width;
return a;
}
public double calculateperimeter(double width , double height)
{
double p = (2*width) + (2*height);
return p;
}
}
copy this code then paste new file:
public class RectangleTest
{
public static void main(String args[])
{
Rectangle ob1 = new Rectangle();
ob1.setcolor("Red");
ob1.setwidth(4.9);
ob1.setheight(8.9);
String c = ob1.getcolor();
double w = ob1.getwidth();
double h = ob1.getheight();
double rectangleArea = ob1.calculateArea(w , h);
double rectanglePerimeter = ob1.calculateperimeter(w , h);
System.out.println("The width of rectangle: " +w);
System.out.println("The height of rectangle: " +h);
System.out.println("The Area of rectangle: " +rectangleArea);
System.out.println("The perimeter of rectangle: " +rectanglePerimeter);
}
}
Task 3: Create
GardeBook class that contains instructorName and courseTitle private
attributes. Define getter and setter methods for both attributes, and another
method named void displayGradeBookInfo(). When this method is
called, it should print courseTitle and instructorName.
Make another class named
GardeBookTest, in its main method, instantiate GradeBook object and call displayGradeBookInfo()
method.
Note: Class
variables should be write-only.
public class GradeBook
{
private String instructorName;
private String courseTitle;
public void setcourseTitle(String tital)
{
courseTitle = tital;
}
public String getcourseTital()
{
return courseTitle;
}
public void setinstructorName(String name)
{
instructorName = name;
}
public String getinstructorName()
{
return instructorName;
}
public void displayGradeBookInfo(String name , String tital)
{
System.out.println("InstructorName:" +name);
System.out.println("CourseTitle:" +tital);
}
{
}
}
=>Copy this code then paste in new file:
public class GradeBookTest
{
public static void main(String args[])
{
GradeBook ob1 = new GradeBook();
ob1.setinstructorName("Muntaha");
ob1.setcourseTitle("Lab Orient programming language");
String n = ob1.getinstructorName();
String t = ob1.getcourseTital();
ob1.displayGradeBookInfo(n , t);
}
}
Task 4: Write a class BankAccount, which have
following attributes:
- Attributes
} accountNumber
} accountTile
Both attributes are private.
Create a test class named BankAccountTest. In main method, instantiate
BankAccount object and display information of account.
Note: Class variables should be read-only.
public class BankAccount
{
private long accountNumber = 12309745;
private String accountTile = "Usman Hashmi";
public long getaccountNumber()
{
return accountNumber;
}
public String getaccountTital()
{
return accountTile;
}
}
=>Copy this code then paste in new file:
public class BankAccountTest
{
public static void main(String args[])
{
BankAccount ob1 = new BankAccount();
System.out.println("Account Number:" +ob1.getaccountNumber());
System.out.println("Account Tital:" +ob1.getaccountTital());
}
}
1 Comments
so sweet
ReplyDelete