







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A java exam covering topics such as inheritance, polymorphism, and cloning. It includes several coding questions that test the student's understanding of these concepts. The exam seems to be closed-book/notes, with a time limit of 60 minutes. Partial credit may be given for partially correct solutions, and the student is required to use correct java syntax. A class hierarchy on pages 15-18 that the student can reference, and there are two additional classes (complex and complextuple) on page 19 that the student must work with. The exam covers a range of topics, including method overriding, exception handling, and time complexity analysis. Overall, this document appears to be a comprehensive assessment of the student's java programming skills and understanding of object-oriented programming principles.
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Name: ___________________________________________________ ISU NetID (username): ______________________________ Recitation section (please circle one): A. T 12:40 pm (Md. Mokarram Chowdhury) B. R 12:40 pm (Md. Arafat Hossain) C. R 8:00 am (Jobayer Ahmmed) D. T 8:00 am (Jobayer Ahmmed) E. T 9:30 am (Hangjun Piao) F. T 4:10 pm (Md. Mokarram Chowdhury) G. R 9:30 am (Hangjun Piao) H. R 4:10 pm (Fahmida Imrose) J. T 2:10 pm (Fahmida Imrose) K. R 2:10pm (Md. Arafat Hossain) Closed book/notes, no electronic devices, no headphones. Time limit 60 minutes. Partial credit may be given for partially correct solutions.
problem: The method calculateInterest(int) is undefined for the type CheckingAccount InterestBearing chad = new CheckingAccount(“Chad Danforth”, 5, 777.77); System.out.println(chad.getInterestRate());
problem: Type mismatch: cannot convert from CheckingAccount to InterestBearing
b) Add a method to the ComplexTuple class that overrides the equals() method from java.lang.Object to perform a deep comparison between this object and an existing ComplexTuple object. Two Complexes are considered to be equal if they have the same real and imaginary parts. You can assume that Complex has a correctly implemented equals() method. @ Override public boolean equals (Object o) { // TODO
c) public static void main( String [] args) { int[] arr = {0,2,4,1,3}; boolean result = hasI(arr, 0); System.out.println(“Result: “ + result); } public static boolean hasI( int[] arr, int i) { if (i == arr.length) { return false; } return arr[i] == ‘i’? true : hasI(arr, i+1); } Number of recursive calls to hasI: O( 𝒏) Worst-case execution time: O( 𝒏)
b) Answer: SELECTIONSORT 0 6 5 7 4 1 2 3 0 6 5 7 4 1 2 3 0 1 5 7 4 6 2 3 0 1 2 7 4 6 5 3 0 1 2 3 4 6 5 7 0 1 2 3 4 6 5 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
a) Fill in the missing row in this INSERTIONSORT output. 1 6 4 7 0 5 3 2 1 6 4 7 0 5 3 2 1 4 6 7 0 5 3 2 1 4 6 7 0 5 3 2 0 1 4 6 7 5 3 2 0 1 4 5 6 7 3 2 0 1 3 4 5 6 7 2 0 1 2 3 4 5 6 7
interface InterstBearing { double getInterestRate(); void setInterestRate( double newRate); void calculateInterest( int time); } public abstract class Account { protected String name; protected int number; protected double balance; public Account( String name, int number, double initialBalance) { this .name = name; this .number = number; balance = initialBalance; } public String getName() { return name; } public int getAccountNumber() { return number; } public double getBalance() { return balance; } public void deposit( double amount) { System.out.println(“Deposited: “ + amount); balance += amount; } public void withdraw( double amount) { if (amount > balance) { throw new IllegalArgumentException(); } System.out.println(“Withdrew: “ + amount); balance - = amount; }
public abstract String getAccountInfo(); } class SavingsAccount extends Account implements InterestBearing { protected double rate; public SavingsAccount( String name, int number, double initialBalance, double interestRate) { super (name, number, initialBalance); rate = interestRate; @Override public double getInterestRate() { return rate; } @Override public void setInterestRate( double newRate) { System.out.println(“Rate changed to: “ + newRate); rate = newRate; } @Override public void calculateInterest( int time) { System.out.println(“Calculating Interest on:” + balance + “ at “ + rate + “% over “ + time
public class Complex implements Cloneable { private int re; // real part private int im; // imaginary part public Complex ( int re, int im) { this. re = re; this. im = im; } @Override public boolean equals (Object o) { // Assume this is already implemented ; the // implementation details are irrelevant. } } public class Complex Tuple { private Complex c1 ; private Complex c2 ; public Complex Tuple ( Complex c1 , Complex c2 ) { this .c1 = c1 ; this .c2 = c2 ; } }