C3 P1

class A{
byte bitval1 = 3;
byte bitval2 = 4;
static byte sumIt(byte a, byte b){
return a + b;
}
}

class B{
A obj = new A();
byte result = A.sumIt(obj.bitval1,obj.bitval2);

byte c = 2;
byte d = 5;

// c += 4;
//d = d + 4;
}

public class c3p1{
public static void main(String[] args){
}
}
  • Why will the code above not compile? Fix the error (in the c3p1 class) so that it does compile, and make the program print the result to the screen.
  • Now erase the double slashes // from the code involving c and d. Compile the code. Why is there an error occurring since the code is equivalently doing the same operation on c and d? What is the reason for this error, and what is so different between how we are changing the value of c versus how we are changing the value of d? Fix the error so that the code compiles.

C3 P2

class B{}
class A extends B{};

class c3p2{
public static void main(String[] args){
A obj1 = new B();
B obj2 = new A();
}
}
  • What is the error in the code above? Explain your answer. (Your answer needs to be more than what the compiler is telling you.)

C3 P3

class c3p3{
int x = 5;
void Joo(){
int y = 7;
y = JooUp();
}
int JooUp(){
return y++;
}

public static void main(String[] args){
c3p3 obj = new c3p3();
obj.Joo();
}
}
  • Explain the problem with the code above. What kind of error will you get? (Compiler? RunTime?) What is the difference between a compiler error and a runtime error?
  • Re-write the code so that JooUp increments the value of y successfully. Make sure your code compiles, and prints the value of y to the screen. (I am looking for you to rewrite the code in such a way that it solves the scoping problem, with a little change to the original code as possible.)
class User{
private int accountID,routingNum;
private float balance, lastDepositAmount, lastWithdrawalAmount;
private String[] info = new String[9]; //info(0) = First name, info(1) = middle name,
//info(2) = Last Name, info(3) = Street Address,
//info(4) = State, info(5) = Zip Code, info(6) = Tel
//info(7) = email address, info(8) = Description
private String creationDate,terminationDate;
private String lastDepositDate, lastWithdrawalDate;

static void getBalance(User user){System.out.println(user.balance);}
static void getCreationDate(User user){System.out.println(user.creationDate);}
static void setCreationDate(User user, String date){user.creationDate = date;}
static void setTerminationDate(User user, String date){user.terminationDate = date;}
static void getTerminationDate(User user){System.out.println(user.terminationDate);}
static void setInfo(User user, String... input){
for(int i=0;i //for(int i=0;i }
static void getInfo(User user){
for(int i=0;i System.out.println(user.info[i]);
}
}
static void setDeposit(User user, float deposit,String date){user.balance+= deposit;user.lastDepositAmount = deposit;user.lastDepositDate = date;}
static void setWithdrawal(User user,float withdrawal,String date){
if(withdrawal > user.balance){
System.out.println("Withdrawal amount exceeds balance.");
}
else{
user.balance-= withdrawal; user.lastWithdrawalAmount = withdrawal;
user.lastWithdrawalDate = date;
}
}
static void setAccountID(User user, int accountNum){user.accountID = accountNum;}
static void getAccountID(User user){System.out.println(user.accountID);}
static void setRoutingNum(User user, int routing){user.routingNum = routing;}
static void getRoutingNum(User user){System.out.println(user.routingNum);}
static void getLastDeposit(User user){System.out.println("Date of Deposit: "+user.lastDepositDate + " Amount "+ user.lastDepositAmount);}
static void getLastWithdrawal(User user){System.out.println("Date of Withdrawal "+user.lastWithdrawalDate+" Amount "+user.lastWithdrawalAmount);}
}

class c3p4{
public static void main(String[] args){
User user1 = new User();
createAccount();
user1.getBalance(user1);
}
static void createAccount(){
User user1 = new User();
User.setAccountID(user1, 1234567);
User.setRoutingNum(user1, 9872345);
User.setInfo(user1,"John","C","Partridge","123 Liberty Way","VA","24502","555-3456","jcpartri@liberty.edu","This is a new account holder.");
User.setDeposit(user1, 500.00f,"01/01/2010");
User.setCreationDate(user1, "01/01/2010");
}
}
  • One day, Mr. Partridge came by the bank to establish an account and invest $500 into the account. But when he went home to check his account online, the balance registered $0.00 dollars. Observing Justin's code, why did this happen?
  • How can the problem be fixed? Fix the problem and then add println methods to the main function to show that the problem is fixed for all entries made into Mr. Partridge's account.
  • What is the advantage/disadvantage to making all the methods in the User class static? Does it create more or less work on the part of the programmer? Explain your answer.
  • Copy and paste the code above into a new text document and name that document c3p5.java. (You will have to rename the c3p4 class to c3p5) Alter the createAccount method so that it returns a type User, and the User object user1 is created in the same line of code in the main method. Then print all the results to the screen. Why does this way you tried work but not what Justin tried in step 1 before you altered his code?
  • Extra Credit[optional] Copy the code above and paste it into a new text file named c3Extra.java. Alter the code so that the user has the ability to create an array of accounts from the command prompt. Also, "clean up" the way the user enters information for each account, such as First Name, Middle, Last Name, etc...
  • Extra Credit[optional] List areas of weakness in Justin's code, then Fix them. (Make them more efficient)
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.