0% found this document useful (0 votes)
3 views12 pages

Unit 5 Test

The document contains a series of programming questions focused on class definitions, method implementations, and object-oriented programming concepts in Java. It includes multiple choice questions related to the functionality of classes like NumberPair and Person, as well as a free response section requiring the implementation of a BankAccount class. The questions test understanding of constructors, method overloading, and object manipulation in Java.

Uploaded by

coconutboy303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Unit 5 Test

The document contains a series of programming questions focused on class definitions, method implementations, and object-oriented programming concepts in Java. It includes multiple choice questions related to the functionality of classes like NumberPair and Person, as well as a free response section requiring the implementation of a BankAccount class. The questions test understanding of constructors, method overloading, and object manipulation in Java.

Uploaded by

coconutboy303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit 5: Writing Classes​ ​ ​ ​ ​ ​ ​ Name: Adhit Natla

Test​ ​ ​ ​ ​ ​ ​ ​ ​ ​

Multiple Choice

Use the following class definition for problems 1 – 2.

public class NumberPair


{
private int num1;
private int num2;

public NumberPair()
{
​ num1 = 0;
​ num2 = 0;
}

public NumberPair(int a, int b)


{
​ num1 = a;
​ num2 = b;
}

public int getFirst()


{
​ return num1;
}
public int getSecond()
{
​ return num2;
}

public void setFirst(int a)


{
​ num1 = a;
}
public void setSecond(int b)
{
​ num2 = b;
}
}
___C__ 1) Consider the following two lines of code, that appear in a class other than NumberPair.

int first = (int)(Math.random() * 10) + 1;


int second = (int)(Math.random() * 10) + 1;

Which of the following represents the correct way to construct a new NumberPair object with two random
digits, each between 1 and 10?

I.
NumberPair example = new NumberPair();
example.setFirst = first;
example.setSecond = second;

II.
NumberPair example = new NumberPair(first, second);

III.
NumberPair example = new NumberPair();
example.setFirst(first);
example.setSecond(second);

(A) I and II only


(B) II only
(C) II and III only
(D) III only
(E) I, II, and III are all correct
_D____ 2) A NumberPair object called myPair was created and num1 equal to 5 and num2 equal to 5.
Which of the following code segments correctly changes num2 to a random value between 1 and 10, and not
equal to num1?

(A)
while(myPair.num1 == myPair.num2)
{
​ int s = (int)(Math.random() * 10) + 1;
​ myPair.num2 = s;
}

(B)
while(NumberPair.num1 == NumberPair.num2)
{
​ int s = (int)(Math.random() * 10) + 1;
​ NumberPair.num2 = s;
}

(C)
while(myPair.getFirst.equals(myPair.getSecond))
{
​ int s = (int)(Math.random() * 10) + 1;
​ myPair.setSecond = s;
}

(D)
while(myPair.getFirst() == myPair.getSecond())
{
​ int s = (int)(Math.random() * 10) + 1;
​ myPair.setSecond(s);
}

(E)
if(myPair.getFirst() == myPair.getSecond() && (s < 1 || s > 10))
{
​ int s = (int)(Math.random() * 10) + 1;
​ NumberPair.num2 = s;
}
___C__ 3) Consider the following class definition.

public class Person


{
private String name;
private static int num;

public Person(String n)
{
name = n;
num++;
}

public boolean equals(Person n)


{
​ if((this.name).equals(n.name))
​ {
​ ​ num--;
​ ​ return true;
​ }
​ return false;
}
}

The following code segment appears in a class other than Person.

Person A = new Person(“Eliza”);


Person B = new Person(“Eliza”);
Person C = new Person(“Eliza”);
System.out.println(A.equals(B));

What is the value of num after the code above executes?

(A) 0
(B) 1
(C) 2
(D) 3
(E) 4
__B___ 4) Consider the following method.

public static String testIt(int a, int b, int c)


{
​ String result = "";
​ if(a < b || b > c)
​ {
​ ​ result += "1";
​ ​ if(a > c)
​ ​ {
​ ​ ​ result += "A";
​ ​ }
​ }
​ if(a != b && b > c)
​ {
​ ​ result += "2";
​ ​ if(a > 4)
​ ​ {
​ ​ ​ result += "B";
​ ​ }

​ }
​ else
​ {
​ ​ result += "3";
​ }
​ return result;
}

What is returned with the method call testIt(2, 7, −2); ?

(A) 1A2
(B) 1A2B
(C) 2B
(D) 13
(E) 23
__C___ 5) Consider the following class declaration.

public class Person


{
​ private String name;
private int age;

// constructors not shown

​ public int getAge()


{
return age;
}
public String getName()
{
​ return name;
}

​ public boolean equals(Person p)


{
return /*implementation not shown*/
}
}

Which of the following correctly implements the equals method of Person?

I. (this.name).equals(p.name) && this.age == p.age;


II. (this.name).equals(p.getName()) && this.age == p.getAge();
III. name.equals(p.getName()) && age == p.getAge();

(A) I only
(B) I and III only
(C) II and III only
(D) I and II only
(E) I, II, and III
Use the following class definition for problems 6 – 7.

public class NumberPair{


private int num1;
private int num2;

public NumberPair()
{
​ num1 = 0;
​ num2 = 0;
}

public NumberPair(int a, int b)


{
​ num1 = a;
​ num2 = b;
}

public int getFirst()


{
​ return num1;
}
public int getSecond()
{
​ return num2;
}

public void setFirst(int a)


{
​ num1 = a;
}
public void setSecond(int b)
{
​ num2 = b;
}
}

public class CoordinatePoint{


​ private NumberPair myPair;

​ public CoordinatePoint(int x, int y)


​ {
​ ​ /*implementation not shown*/
​ }

​ public String getQuadrant()


​ {
​ ​ /*implementation not shown*/
​ }

}
__E___ 6) Which of the following is the correct implementation for the CoordinatePoint parameter
constructor, where the parameter x represents the x-coordinate, and parameter y represents the y-coordinate?

(A)
myPair = NumberPair(x, y);

(B)
myPair.setFirst(x);
myPair.setSecond(y);

(C)
x = myPair.getFirst();
y = myPair.getSecond();

(D)
NumberPair myPair = new NumberPair(x, y);

(E)
myPair = new NumberPair();
myPair.setFirst(x);
myPair.setSecond(y);

__B___ 7) Consider the following implementation for the getQuadrant() method, that returns “Q1” if x
and y are both positive, “Q2” if x is negative and y is positive, “Q3” if both x and y are negative, and “Q4” if x
is positive and y is negative. If either point is a 0, the function should return “on axis”.

if(x > 0 && y > 0)


​ return “Q1”;
else if(x < 0 && y > 0)
​ return “Q2”;
else if(x < 0 && y < 0)
​ return “Q3”;
else if(x > 0 && y < 0)
​ return “Q4”;
else
​ return “on axis”;

Which statement below is correct?

(A) The implementation will only work is all instances of x are replaced with myPair.num1 and all instances
of y are replaced with myPair.num2.
(B) The implementation will only work if all instances of x are replaced with myPair.getFirst() and all
instances of y are replaced with myPair.getSecond().
(C) The implementation will only work is all instances of x are replaced with myPair.x and all instances of y
are replaced with myPair.y.
(D) The implementation will only work is all instances of x are replaced with this.x and all instances of y
are replaced with this.y.
(E) The implementation works correctly as it is.
Questions 8 − 9 refer to the following class.

public class Line {


​ private int slope;
​ private int yInt;

​ public Line(int m, int b) {


​ ​ slope = m;
​ ​ yInt = b;
​ }

​ public int getSlope() {


​ ​ return slope;
​ }
​ public in getYInt() {
​ ​ return yInt;
​ }

​ //postcondition: returns true if the x and y pair in the parameter


​ //lie on the line.
​ public boolean onLine(int x, int y) {
​ ​ /*implementation not shown*/
​ }
}

___E__ 8) A point will lie on the line if the equation y = mx + b (where m is the slope and b is the yInt)
evaluates to true. Which of the following correctly implements this method?

I. return y == slope * x + yInt;


II. return this.y == slope * this.x + yInt;
III. return y == this.slope * x + this.yInt;

(A) I only
(B) II only
(C) II and III only
(D) III only
(E) I and III only

___B__ 9) Assume the following code is created in a class outside of Line.

Line linear = new Line(2, 5);

Which of the following correctly displays y = 2x + 5 to the screen?

(A) System.out.println(“y = “ + Line.getSlope() + “x + “ + Line.getYInt());


(B) System.out.println(“y = “ + linear.getSlope() + “x + “ + linear.getYInt());
(C) System.out.println(linear.onLine(2, 5));
(D) System.out.println(“y = linear.getSlope() * x + linear.getYInt()”);
(E) System.out.println(“y = “ + linear.slope + “x + “ + linear.yInt);
___D__ 10) Consider the following methods.

public static int perimeter(int a, int b){


​ /*implementation not shown*/
}
public static int perimeter(int a, int b, int c){
​ /*implementation not shown*/
}
public static double perimeter(double a, double b){
​ /*implementation not shown*/
}

Which of the following methods could be added to the above methods without a compiler error?

(A) public static double perimeter(int a, int b)


(B) public int perimeter(int a, int b){
(C) public static int perimeter(int x, int y, int z)
(D) public static int perimeter(double a, double b, double c)
(E) private static double perimeter(double a, double b)
Free Response

11) You will be writing a complete BankAccount class. This class will have the following properties:

●​ Two instance variables, an int called accountNumber to represent an account number between 1000
and 9999, and a double called balance to represent the amount of money in the bank account.
●​ A parameter constructor that accepts a parameter that represents the starting amount in the bank account.
In this parameter constructor, a random accountNumber should be generated and assigned to
accountNumber.
●​ An accessor method for accountNumber.
●​ A method called deposit(double amount) that deposits the amount into your bank account
balance.
●​ A method called withdraw(double amount) that checks first if you have enough money in your
bank account to withdraw the amount.
o​ If you do, the method withdraws that amount
o​ If you do not, the method prints “Insufficient Funds”
●​ A method called printBalance() that prints the current amount you have in your bank account.
●​ A method called transfer(BankAccount recipient, double amount) that will transfer
amount from your bank account to the bank account of recipient. If you do not have enough money to
transfer from your account, this method will print “Insufficient Funds”.

The following table contains a sample code execution sequence and the corresponding results.

Statements and Expressions Comment


ash has a bank account with $75.80 in it
BankAccount ash = new BankAccount(75.80) and a random generated account number of
1445
ron has a bank account with $25.45 in it
BankAccount ron = new BankAccount(25.45) and a random generated account number of
7888
System.out.println(ash.getAccountNumber()); 1445

ash.deposit(7.89); deposits $7.89 into ash’s account

System.out.print(ash.printBalance()); Current Balance: $83.69

ash.withdraw(100); Insufficient Funds

ash.withdraw(40); withdraws $40 from ash’s account

System.out.print(ash.printBalance()); Current Balance: $43.69

ash.transfer(ron, 50); Insufficient Funds

ash.transfer(ron, 20); Transferred $20 to 7888

System.out.print(ash.printBalance()); Current Balance: $23.69


System.out.print(ron.printBalance()); Current Balance: $45.45

Write the complete BankAccount class, including the constructor and any required instance variables and
methods. Your implementation must meet all specifications and conform to the example.

public class BankAccount {


private int accountNumber;
private double balance;

public BankAccount(double startingBalance) {


balance = startingBalance;
accountNumber = (int)(Math.random() * 9000) + 1000; // 1000-9999
}

public int getAccountNumber() {


return accountNumber;
}

public void deposit(double amount) {


balance += amount;
}

public void withdraw(double amount) {


if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient Funds");
}
}

public void printBalance() {


System.out.println("Current Balance: $" + String.format("%.2f", balance));
}

public void transfer(BankAccount recipient, double amount) {


if (balance >= amount) {
balance -= amount;
recipient.deposit(amount);
System.out.println("Transferred $" + String.format("%.2f", amount) + " to " +
recipient.getAccountNumber());
} else {
System.out.println("Insufficient Funds");
}
}
}

You might also like