Sei sulla pagina 1di 5

Qatar University Dept.

of Computer Science & Engineering

Object-Oriented Programming (CMPS252)

Lab 05
Methods & Arrays in Java
Objectives:
At the end of this lab session the student should be able to: Create and call methods (instance and static methods). Use predefined class and methods (Random class). Use methods overloading. Use arrays in Java.

Exercise 1 1.1 Trace the following method to figure out its task.
public int digits(int x) { double n=0; while (x!=0){ x=x/10; n++; } return n; }

The method above returns ____________________________. 1.2 Does the above method has a compilation error? ____________________ 1.3. Create a new project Lab5Proj1, and create new class IntNumbers. After fixing the error in method digits, copy that method to your class. 1.4 Your main method in this class should look as follows
public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); System.out.println(digits(n));

}
1.5 Do you have any compilation error as a result of calling digits method? Yes [ ] No[ ] If yes , what is the problem ?___________________________________________ 1.6 Add the word static after word public and before int in method digits.

1.7 It is concluded that Static methods (like main method) only call ___________________________________ 1.8 Does it matter if you put digits method before or after the main method? Yes[ ] No[ ] 1.9 How can we define the variable n twice in our program (in the main and in digits method)? __________________________________________________________________ __________________________________________________________________ 1.10 Create a new class call it IntNumbersTest, move the main method from IntNumbers to this new class.( now class IntNumbers only includes digits method) Are we still able to call method digits now ? Yes[ ] No[ ] 1.11 If the method digits still static in class IntNumbers, which of the following is the correct way to call the method digits a) IntNumbers num=new IntNumbers(); System.out.println(num.digits(n)); b) System.out.println(IntNumbers.digits(n)); The correct answer _______________________________ 1.12 It is concluded that There are three ways to call a method: 1)__________________________________________ 2)__________________________________________ 3)__________________________________________

Exercise 2) : In this exercise, we build an application that helps elementary school students learn multiplication: 2.1. Create a new project and call it Lab5Proj2, and then create a new class and call it RandomApp. 2.2 Using class Random, write a method generateRandom that returns a random number between 1 and 10. Note: Consult the API to explore which method of class Random to use. 2.3 Write a method called generateQuestion which uses method generateRandom to generate random numbers, then using JOptoinPane methods asks the user with a question such as How much is 6 times 7? If the student inputs a correct answer, the method displays the message Correct. Very good! and returns true; otherwise, the method displays Incorrect answer and returns false. Note: Refer to Exercise 2 of Lab03 (and the API), to recall how to use the JOptionPane class. 2

2.4 In the main method, use showInputDialog method to ask the user how many questions s/he wants to take. Based on the entered number, use for loop to call generateQuestion method. At the end, display how many correct and incorrect answers. Exercise 3)

Arrays in Java
In java, arrays are processed as objects so you need first to create that array using the keyword new like any other objects. For example to declare array of 10 integers : int x[ ]=new int[10]; The other way to declare an array is by array initializer where the array length is determined by the number of elements in the list. For example int x[ ]={4,5,2,88,3}; Because an array is like objects, it has a property called length which represents the number of array elements, for example int elements=x.length;

3.1 Create a new project and call it Lab5Proj3, and create a new class and call it ArrayApp, copy the following code to your class:
import javax.swing.JOptionPane; public class ArrayApp { public static void main(String[] args) { int a[]=new int[10]; for(int i=0;i<a.length;i++) { String number=JOptionPane.showInputDialog("Enter element no. "+i); a[i]=Integer.parseInt(number); } }

3.2 Add a method called getArrayAvg that receives array of integers and returns average of that array. 3.3 Add a method replace (int a[ ],int x,int y) that replaces all x elements in array a with element y. 3.4 Call this method in your main and print array elements to see if they were changed. Can you conclude whether the array is passed to the method by value or is it by reference? __________________________________________________ 3.5 Add a method called replace (int a[ ], int x) that replaces all x elements in array a with zero. 3.6 Call this method to see its effects on the array . 3.7 As you can see, we have two methods with same name replace in one class but their signature (parameters) are different. We call this ______________________________________________________. 3

3.8 Can we have two methods with the same name, same parameters but different return types? Yes[ ] No[ ] Exercise 4)

Array of Objects
4.1 Create a new project and call it Lab5Proj4, and create a new class called Student which has the following instance variables: String name double mathGrade double scienceGrade double langGrade 4.2 Add a constructor that receives four parameters (string and 3 double values). 4.3 Add setter/getter methods. If any grade is not positive, it should be set to 0.0. 4.4 Add a method getAvg that returns the average of student. 4.5 Create a new class and call it ObjectArrayTest, and copy the following code
import javax.swing.JOptionPane;
public class ObjectArrayTest{ public static void main(String arg[]) { Student st[]=new Student[5]; for(int i=0;i<st.length;i++) { String studentName=JOptionPane.showInputDialog("Enter student name"); String sgrade1=JOptionPane.showInputDialog("Enter math grade"); double grade1=Double.parseDouble(sgrade1); String sgrade2=JOptionPane.showInputDialog("Enter Science grade"); double grade2=Double.parseDouble(sgrade2); String sgrade3=JOptionPane.showInputDialog("Enter lang. grade"); double grade3=Double.parseDouble(sgrade3); st[i]=new Student(studentName,grade1,grade2,grade3); }

}
}

4.6 Add a method display that prints students names, their grades and their averages. 4.7 Add a method highest (Student students[] ) that returns string that represents the name of the student with highest average grade in the array. 4.8 Can we change length of the array student instead of 5 to be a variable that is entered by user at the beginning of runtime. Yes[ ] No[ ] Is it the same technique in C++?! Yes[ ] No[ ] _______________________

4.9 Add a method avgOfAll(Student students[ ]) that returns average of all students grades. 4

Additional Exercises Create a new class called Bank and in the main method use class Account (lab 4) to create an array of 10 accounts. Enter data for all accounts (account number,name, balance). Add method getTotal(Account a[ ]) that returns total of all balances. Add method addInterest(Account a[], double x) that deposit amount x to all accounts in array a.

Potrebbero piacerti anche