Sei sulla pagina 1di 5

COMP 248 - Tutorial #10

Question 1:Consider the following class:


public class AClass { private int a; public int b; public AClass() { a = 10; b = 10; } private void increment() { increment(1); } public void increment(int i) { a+= i; b+= i; } }

and the following declaration in the driver class:


AClass obj1 = new AClass();

Indicate if the following instructions will cause a syntax error if they are placed in the driver class after the above declaration. If there is an error, briefly explain why. A. System.out.print(obj1); B. AClass.increment(5+5); C. System.out.print(obj1.a); D. System.out.print(obj1.b);
Question 2: Given the following class definition
public class Question { private int gradeQ1; private int gradeQ2; private int gradeQ3; private int total; public void questionTotal(){ } public int returnTotalGrade{ }

public boolean getQuestionRight(){ } }

A. What is the return type of the method questionTotal()? B. What is the return type of the method getQuestionRight()? C. Complete the method questionTotal() so that it returns the content of total. D. Complete the method returnTotalGrade() which calculates the total score (sum of gradeQ1, gradeQ2and gradeQ3).
Question 3: What will be the output of the following code.
import java.util.*; class RandomTest { public static void main (String args[]) { int[] ndigits = new int[10]; double x; int n; Random myRandom = new Random(); // Initialize the array for (int i = 0; i < 10; i++) { ndigits[i] = 0; } // Test the random number generator a whole lot for (long i=0; i < 100000; i++) { // generate a new random number between 0 and 9 x = myRandom.nextDouble() * 10.0; n = (int) x; //count the digits in the random number ndigits[n]++; } // Print the results for (int i = 0; i <= 9; i++) { System.out.println(i+": " + ndigits[i]); } } }

Question 4- Assume that you need to write a class to represent a polynomial of degree 2. Recall that a polynomial of degree 2 is an expression of the form a2x2 + a1x + a0, where a0, a1 and a2 are real numbers and x is a real variable. The attributes of the class have already been defined like this: public class PolyDegree2 { double a0; double a1; double a2; } Write the header of the following methods: A- A method to compute the 1st derivative of a polynomial. Recall that the 1st derivative of a polynomial of degree 2 can be represented by a polynomial of degree 2 (with a2=0). For example the derivative of 2x2+3x is 4x+3. B- A method to determine if a polynomial is equal to another. Recall that 2 polynomials are equal if the value of their respective a0, a1 and a2 are equal. For example, 10x2+5x+1.5 is equal to 10x2+5x+1.5. C- A method to evaluate the polynomial with a given value of x. For example, the evaluation of 10x2+5x+1.5 with x=2 is 51.5. D- A method to display a polynomial in a nice format. E- A method to add 2 polynomials. For example, (2x2-4) + (x2+3x-3) is (3x2+3x-7). Question 5A- Write a complete class called GasPump to represent a gas pump. The class should have a constructor to set the amount of available gas supply (in litres) and the price of gas per litre. The class interface should include: - accessor and mutator methods to set and get the gas price; - a method to add fuel to the gas supply. Note that the maximum capacity of the pump is 5000 litres. If we try to add too much fuel, once the maximum capacity is reached, no more fuel can be added; - an accessor method to get the current amount of gas supply; - a method that sells a specific amount of gas. This method should reduce the supply of available gas by the amount sold and return the total cost of the gas sold. If the supply of the available gas is less than the amount requested, then only the existing supply should be sold. B- Using the class defined in part A, write a driver program that: creates an object called shell of the class GasPump and initializes it to 3000 litres and 78.5 cents a litre; asks the user how much gas he/she wants to buy; sells this amount of gas from shell; adds 500 litres of gas to shell; displays the content of gas supply left in shell.

Question 6- Create a Java class named Book with instance variables title, author, ISBN, and yearPublished. Add accessor and mutator methods to the Book class. Question 7- Complete the following class definition to represent a point in a 2dimentional space.
public Class Point { private int x; // x-coordinate private int y; // y-coordinate

/* A- Write a constructor to set the coordinates of the point to two specific values x1 and y1 which are passed from the driver. */ /* B- Write two accessor methods: One to return the content of the x coordinate, the other to return the content of the y coordinate. */ /* C- Write two mutator methods: One to set the content of the x coordinate to some value passed from the driver and one to set the y coordinate to some value which again is passed from the driver. */ /* D- Write a method which will return true if two points have the same coordinates and false otherwise. */ /* E- Write a method calledreverse which will return a new point with the coordinates reversed. That is, if the point which invokes the method has coordinates (a, b), then the method should return a new point with coordinates (b, a). */ /* F- Write a method calledmoveBy which will move a point by an integer value. The method will add to each coordinate the value passed as argument. So if the original point was (x1, y1), after this method is invoked it will have the coordinates (x1+a, y1+a), where a is the argument (the integer value). */ /* G- Write the toString method such that it displays an object in the following format: Coordinates of point are (x, y) where x and y are the contents of the instance variables. */
} // end of class Point

Complete the following driver program which tests the class Point defined above.
public class PointTest { public static void main(String[] args) { /* H- Declare 2 points:p1 with

coordinates (0,0) and p2 with coordinates (2,3). */

/* I- Write the necessary statement(s) to display the coordinates of p1 and p2. */ 4

/* J- Write a statement to reverse the coordinates of p2. */ /* K- Write the necessary statement(s) to set the coordinates of p1 to be the reverse of p2. For example, if p1 is (1,2) and p2 is (2,3) then the coordinates of p1 will be changed to (3,2). */ /* L- Write a statement to add 10 to both coordinates of p1.*/ /* M- Write the necessary statements to compare the coordinates of points p1 and p2 and print Same if they have the same coordinates and Different if they dont have the same coordinates. */
} // end of class PointTest

Potrebbero piacerti anche