In your own words, describe what distinguishes a class variable from an instance variable. (I'm not asking about how you distinguish them syntactically in Java --- I'm asking how their behavior is different.)
What is the purpose of a constructor method?
Write a program that reads a line from the user and says ``ok'' if the user's line represents a valid integer, and ``bad'' otherwise.
Type something. -56a2 bad
import csbsju.cs160.*; public class Example { public static void main(String[] args) { } }
[Covers material not covered this semester yet.]
Complete the following definition of a bank. The bank should be able to support up to 100 accounts. Do not worry about what happens if a 101st account is added to the bank.
public class Bank { Account[] acct; int num_acct; public Bank() { } public int getNumAccounts() { return num_acct; } public Account getAccount(int which) { // returns the whichth account in the bank } public void addAccount(Account what) { // adds what into the bank } }
Recall the Account definition methods.
public class BankFunctions { public static double totalAssets(Bank bank) { } }
Name the primitive types of Java. (There are eight of them.)
[Covers material not covered this semester.]
Consider the following two class definitions.
class X { public double g(double x) { return f(x) * f(x); } public double f(double x) { return x + 1.0; } } class Y extends X { public double f(double x) { return x + 2.0; } }
Y y = new Y(); X x = y; IO.println(y.f(2.0)); IO.println(x.f(2.0)); IO.println(x.g(2.0));
import csbsju.cs160.*; public class Example { public static void main(String[] args) { try { IO.print("Type something. "); Integer.parseInt(IO.readString()); IO.println("ok"); } catch(NumberFormatException e) { IO.println("bad"); } } }
public class Bank { Account[] acct; int num_acct; public Bank() { acct = new Account[100]; num_acct = 0; } public int getNumAccounts() { return num_acct; } public Account getAccount(int which) { // returns the whichth account in the bank return acct[which]; } public void addAccount(Account what) { // adds what into the bank acct[num_acct] = what; ++num_acct; } }
public class BankFunctions { public static double totalAssets(Bank bank) { double ret = 0.0; for(int i = 0; i < bank.getNumAccounts(); i++) { ret += bank.getAccount(i).getBalance(); } return ret; } }
4.0 4.0 16.0