Lab Manual: CLO No. Learning Outcomes Assessment Item BT Level PLO
Lab Manual: CLO No. Learning Outcomes Assessment Item BT Level PLO
Lab Manual
OOP
3rdSemester Lab-06: Classes and Methods in Java
Laboratory 06:
Statement Purpose:
At the end of this lab, the students should be able to:
Arrays Class:
The Arrays class, available in the java.util package, is a helper class that contains methods for working
with arrays. These methods can be used for modifying, sorting, copying, or searching data. These
methods that we use are static methods of the Array class. (Static methods are methods that can be
called without creating an instance of a class.)
import java.util.Arrays;
public class ArrayMethods {
public static void main(String[] args) {
int[] a = {5, 2, 4, 3, 1};
System.out.println("Before Sorting " +Arrays.toString(a));
Arrays.sort(a);
System.out.println("After Sorting " +Arrays.toString(a));
Arrays.fill(a, 8);
System.out.println("After filling the sorted array a with value 8 ->"
+Arrays.toString(a));
int[] b = Arrays.copyOf(a, 5);
System.out.println("After copying first 5 elements of array a in array b -
>" +Arrays.toString(b));
if (Arrays.equals(a, b)) {
System.out.println("Arrays a, b are equal");
} else {
System.out.println("Arrays a, b are not equal");
}
}
}
Output:
In the above code example, we have used five methods of the Arrays class.
import java.util.Arrays;
Arrays.sort(a);
The sort() method sorts the integers in an ascending order.
System.out.println(Arrays.toString(a));
The toString() method returns a string representation of the contents of the specified array.
Arrays.fill(a, 8);
The fill() method assigns the specified integer value to each element of the array.
The copyOf() method copies the specified number of elements to a new array.
if (Arrays.equals(a, b)) {
System.out.println("Arrays a, b are equal");
} else {
System.out.println("Arrays a, b are not equal");
}
The equals() method compares the two arrays. Two arrays are equal if they contain the same
elements in the same order.
Fields:
Remember that our definition of an Object is that it has both data and behavior. Fields are what allow
us to have data. They should reflect the state of the Object!
Fields are variables that are declared outside of any method/constructor, which makes them accessible
in any method/constructor. This means that fields are great for remembering information across method
calls.
If you want a field to have a different initial value, you should reassign it in the constructor.
A Simple class:
Consider the following code, there is a class called BA that defines three instance variables: width,
height, and depth. Currently, this class does not contain any methods.
class BA {
double width;
double height;
double depth;
}
Output:
Volume is 3000.0
}
}
Output:
Volume is 3000.0
Volume is 162.0
Consider the following code, in which we create a box class with its instance variables: width, height
and depth. This class also defines a method called volume.
Another class called DemoBox is also created, which includes main method. In this class, objects of
class Box are created and with the help of these objects methods of Box class are called as
object_name.volume(), which returns the calculated volume.
Code:
//A program that uses the Box class.
//This program includes a method inside the box class which returns the volume of
a box.
public class Box {
double width;
double height;
double depth;
Output:
Volume is 3000.0
Volume is 162.0
Volume is 3000.0
Volume is 162.0
Accessor:
An Accessor method is commonly known as a get method or simply a getter. They are declared
as public. They are used to return the value of a private field. The same data type is returned
by these methods depending on their private field.
Syntax:
public int getNumber() {
return number;
}
Example:
public class Employee {
private int number;
public int getNumber() {
return number;
}
public void setNumber(int newNumber) {
number = newNumber;
}
}
Mutator:
A Mutator method is commonly known as a set method or simply a setter. A Mutator method
mutates things, in other words change things. It shows us the principle of encapsulation. They
are also known as modifiers. They are easily spotted because they started with the word set.
They are declared as public. Mutator methods do not have any return type and they also accept
a parameter of the same data type depending on their private field. After that it is used to set
the value of the private field.
Syntax:
public void setAge(int Age) {
this.Age = Age;
Example:
public class Employee {
private int Age;
public int getAge() {
return this.Age;
}
public void setAge(int Age) {
this.Age = Age;
}
Example:
//Accessor Method
public String getName() {
return name;
}
//Mutator
public void changeHours(int newHours) {
if(newHours>0&&newHours<200) hours=hours+newHours;
}
//Constructor
public Student(String newStudentName) {
name=newStudentName;
major=“”;
minor=“”;
hours=0;
cumulativeGPA=0.0;
}
//objects of student class
Student s1, s2, s3;
s1=new Student(“Ali”);
Engr. Sidra Shafi Lab-06 7
3rdSemester Lab-06: Classes and Methods in Java
s2=new Student(“Sehrish”);
s3=new Student(“Ahmad”);
s1.changeHours(15);
s2.changeHours(35);
…
//A method which will return the proper class rank as a String.
public String getRank( ) {
if(hours>=90) return “senior”;
else if(hours>=60) return “junior”;
else if(hours>=30) return “sophomore”;
else return “freshman”;
}
/*If a class has a toString method, it will automatically be called when we attempt to output
an object of that class using println. We define the toString to return a String of the relevant
parts of the object.*/
public String toString() {
String tempMajor, tempMinor;
if(!major.equals(“”))
tempMajor=major
else
tempMajor=“undeclared”;
if(minor!=“”)
tempMinor=minor
else
tempMinor=“none”;
return name + “\t” + tempMajor + “\t” + tempMinor
+ getRank();
}
Lab Exercise:
There are four errors in the following code. Find all the errors in the following Point
class.
public class Point {
int x; // Each Point object has
int y; // an int x and y inside.
public void translate(int dx, int dy) { // Shifts this point's x/y
int x = x + dx; // by the given amounts.
int y = y + dy;
}
}
Lab Tasks
Task 1: Marks: 5
Make a class Meters which consists of a public parameterized method named feetToMeters( ).
This method should receive from its caller the number of feet to be converted and should check that this
value is positive. It should convert that quantity to meters by multiplying it by 0.3048 and return the
resulting value to the caller.
Note that where a program typically inputs values from the keyboard, a method
typically receives values from whoever called the method. Similarly, where a program
typically outputs values to the screen, a method typically returns a value to whoever called the method.
Note also that we want to try and anticipate what could go wrong. Since the method will only produce
a correct result if the value it receives is not negative, we say that this method has a precondition -- a
condition that must be true for the method to execute correctly.
The first thing that we must decide is what measurement conversions we wish our class to provide. For
example, the following are just a few of the useful conversions:
Specification:
Task 2: Marks: 5
Make a class Rectangle which calculates the area and perimeter of rectangle according to the
following algorithm.
Step 1: Declare two private variables length and width of type double.
Step 2: Make a Parameterized method which sets the length and width according to the values it receives
as parameters.
Step 3: Make two methods getLength and getWidth which only return length and width. Their return
type is double.
Step 4: Make a value returning method perimeter which calculates the perimeter according to the
formula 2*L+2*W.
Step 5: Make a value returning method area which calculates the area according to the formula L*W.
Step 6: Make a class RectDemo which includes main method. Make an object of Rectangle class.
Step 7: Get and Print the length and width of Rectangle by using appropriate methods.
Step 8: Print the Perimeter and Area of Rectangle.
********