Assume method0 calls method1 and method2, method1 calls method3 which calls method4 and
method5. Explain the chain of method calls (what order methods are called and from which
method.
method0 calls method1 which calls method3 which calls method4, method4 terminates and
method3 calls method5, method5 terminates, method3 terminates, method1 terminates,
method0 calls method2, method2 terminates, method0 terminates.
x = (y++ + 1) - (--z) + 1;
http://www.unf.edu/~broggio/cop2551/Chap04%20EX%20Solutions.pdf
Rewrite the following set of if statements using a nested if-else structure.
if (score >= 90) grade = 'A';
else if (score >= 80) grade = 'B';
else if (score >= 70) grade = 'C';
else if (score >= 60) grade = 'D';
else grade = 'F';
Explain what is meant by short circuiting and provide an example of short circuiting a condition
with && and provide an example of short circuiting a condition with | |.
Short circuiting is when a boolean expression does not have to be completely evaluated because
the final answer is determined with just a partial evaluation. This can occur in an expression with
&& if one part of the expression evaluates to false, and in an expression with | | if one part of
the expression evaluates to true.
Examples:
(x != 0) && (sum / x > 100) can be short circuited if x != 0 is false, preventing a division by
zero error.
(a.equals("yes") | | a.equals("no")) can be short circuited if a is equal to "yes" so that the
computer does not waste time in testing a.equals("no").
The following code has a syntax error immediately before the word else. What is the error and
why does it arise? Fix the code so that this statement is a legal if-else statement.
The error is "else without if" and it arises because of the ";" after the condition but before x++.
The Java compiler determines that the if clause is in fact ; (no statement) and that x++; is a
statement that follows the if statement. Therefore, since x++; is not part of the if statement, the
"else" is felt to be an else without an if, and that is why the error has arise. The statement
should be
if (x < 0) x++;
else x--;
mport java.util.*;
public class Review_2_19_2013{
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
Random rng = new Random();
int n, value, sum, i;
String firstName = "John";
String lastName = "Smith";
System.out.println ("The initials are:\t" +
getInitials(firstName, lastName));
n = rng.nextInt(251);
System.out.println (n + "\t is\t" + isEven(n));
int result = sum (5);
System.out.println ("The sum of values from 1 to 5 is:\t"
+ result);
print (150);
reverse ("VSU");
}
public static String getInitials (String f, String l) {
String result;
result =""+ f.charAt(0)+ l.charAt(0);
return result;
}
public static String isEven (int n) {
if (n%2 == 0)
return "EVEN";
else
return "ODD";
}
public static int sum (int n) {
int total = 0;
int i = 1;
while (i <= n){
total += i;
i++;
}
return total;
}
public static void print (int n) {
int i = 0;
while (i < n){
if (i%4 == 0)
System.out.println();
System.out.print(i + "\t");
i++;
}
}
public static void reverse (String s) {
int size = s.length();
int i = size - 1;
System.out.println("\n\n");
while (i >= 0){
System.out.print (s.charAt(i));
i--;
}
}
}
Accounting
import java.util.*;
public class Account {
private String name;
private double balance;
private Date dateCreated;
public Account(){
name = "unknown";
balance = 0;
dateCreated = new Date();
}
public Account(String n, double b){
name = n;
balance = b;
dateCreated = new Date();
}
public double deposit (double amount){
balance +=amount;
return balance;
}
public double withdraw (double amount){
balance -=amount;
return balance;
}
public String getName(){
return name;
}
public void setBalance (double amount) {
balance = amount;
}
public double getBalance(){
return balance;
}
public void setName (String name) {
this.name = name;
}
public String toString(){
String result="";
result += "\n\n\tMy Bank Report" +
"\n\tOwner:\t\t" + name +
"\n\tBalance:\t"+balance +
"\n\tDate Created:\t"+ dateCreated;
return result;
}
}
test Acc
public class TestAccount{
public static void main (String [] args)
{
//create 3 accounts
Account acct1 = new Account ("Smith",1000);
Account acct2 = new Account ();
Account acct3 = new Account ("Jones",222);
acct1.deposit(300);
acct1.withdraw(15);
acct2.setName("Todd");
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
acct1.deposit(5);
acct3.deposit(10000);
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
© Copyright 2026 Paperzz