Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

GIFT School of Engineering

and Applied Sciences

Fall 2019

CS-242: Data Structures and Algorithms

Lab-1 Manual
Practice

v1.0
3/23/2019

1
Lab-1 Manual 2019

Task #1: Writing Methods in Java


In this task, you are being asked to write a method printArray which takes an Integer array as
an argument, and then prints the values of array on the console. You may use the following
method header:
public void printArray(Integer[] inputArray)
Now, create an overloaded method with the same name to print values from a Double array
argument. You may use the following method header:
public void printArray(Double[] inputArray)
Finally, create another overloaded method with the same name to print values from a String array.
You may use the following method header:

public void printArray(String[] inputArray)

1. Create a class with the name PrintLab1.java.


2. Create appropriate arrays: intArray, doubleArray, stringArray of types Integer,
Double and String.
3. Insert values in all arrays. Do not use a Scanner for any inputs.
4. Make appropriate method calls for above created arrays and print the values.
5. Give appropriate message while printing values.

2
Lab-1 Manual 2019

Task #2: Writing a Method in Java


In this task, you are being asked to write a method printArray which takes an Object array as
an argument, and then prints the values of array on the console. You may use the following
method header:
public void printArray(Object[] inputArray)

1. Create a class with the name PrintLab2.java.


2. Create appropriate arrays: intArray, doubleArray, stringArray of types Integer,
Double and String.
3. Insert values in all arrays. Do not use a Scanner for any inputs.
4. Make appropriate method calls for above created arrays and print the values.
5. Give appropriate message while printing values.

3
Lab-1 Manual 2019

Task #3: Creating Classes and Objects


NOTE: Write your class and the main method in separate files.

Write a class named Student that has the following fields:


 name. The name field references a String object that holds the student’s name.
 id. The idNumber is an int variable that holds the student’s ID number.
 department. The department field references a String object that holds the
name of the department of a Student.
Write appropriate mutator methods that store values in these fields and accessor methods that
return the values in these fields.
This class also contains the following methods:
 void copy(Object obj)
This method copies all data of the caller object to the argument object obj.
 public boolean isEqual(Object obj)
This method return true if both objects are equal

1. Create a program called Student.java for the class, and RunStudent.java having the
main method.
2. Correctly display appropriate messages.

4
Lab-1 Manual 2019

Task #4: Creating Classes and Objects


In this task, you are being asked to write a class and create objects in Java.

NOTE: Write your class and the main method in separate files.

Write a class definition Cube with one data member side (double). The class has the following
methods described below:
 Build a class with private data member side that holds the side value of a cube
 Write the default constructor, as well as a one, two, and three argument overloaded
constructors.
 This class also contains the following methods:
o void copy(Object obj)
This method copies all data of the caller object to the argument object b.
o public boolean isEqual(Object obj)
This method return true if both objects are equal

o public Cube isGreater(Object obj)


This method returns the reference of greater object

o String toString()
This method returns the String representation of the caller object.

1. Create a program called Cube.java for the class, and RubCube.java having the main
method.
2. Create objects using each constructor.
3. Display the state of all objects.
4. Apply the setter methods and change the state of any two objects.
Now, apply all above methods on these two objects while displaying appropriate messages.

5
Lab-1 Manual 2019

Task #5: Creating Classes and Objects


In this task, you are being asked to write a class and create objects in Java. Also, you are being
asked to write appropriate constructors to initialize objects.
NOTE: Write your class and the main method in separate files.

Write a class definition Book with three data member bookId (int), pages (int) and
price (double). The class has the following methods as described below:
 Build a class with three private data member bookId, pages and price for
holding data about books.
 Write the default constructor, as well as a one, two, and three argument overloaded
constructors.
 This class also contains the following methods:
o void display()
Displays all the values of data members of an object with appropriate messages.
o Setter and Getter methods
Create setter and getters methods for each instance variable.
o boolean isLarger(Object b)
This method returns true if the caller object has more pages than the argument
object b, and false otherwise.
o boolean isExpensive(Object b)
This method returns true if the caller object is more expensive than the argument
object b, and false otherwise.
o void copy(Object b)
This method copies all data of the caller object to the argument object b.
o String toString()
This method returns the String representation of the caller object. For example,
suppose that an object has the following state:
bookId = 123, pages = 450, price = 255.99
This method should return a String having the concatenation of all values as:
“123, 450, 255.99”

5. Create a program called Book.java for the class, and RunBook.java having the main
method.
6. Create objects using each constructor.
7. Display the state of all objects.
8. Apply the setter methods and change the state of any two objects.
9. Now, apply all above methods on these two objects while displaying appropriate
messages.

You might also like