Consider the following class SemesterEnrollment to represent the enrollments of students in a semester.
public class SemesterEnrollment {
private int num_students;
private String[] names;
private int[] total_enrolled;
public SemesterEnrollment(int max_students) {
num_students = 0;
names = new String[max_students];
total_enrolled = new int[max_students];
}
public void enroll(String name, int credits) {
for(int i = 0; i < num_students; i++) {
if(names[i].equals(name)) {
total_enrolled[i] += credits;
return;
}
}
names[num_students] = name;
total_enrolled[num_students] = credits;
num_students++;
}
public int getCreditsTaken(String name) {
for(int i = 0; i < num_students; i++) {
if(names[i].equals(name)) {
return total_enrolled[i];
}
}
return 0;
}
}
What to do? add Alice 2 What to do? add Bob 4 What to do? add Alice 4 What to do? add Spot 5 What to do? add Bob 1 What to do? query Bob 5 What to do? query Alice 6 : (This program continues asking questions indefinitely)
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) {
}
}
Consider the following two class definitions.
class Test { public int a; public Test() { a = 8; } public Test(int x) { a = x + 4; } }class Example extends Test { public int b; public Example() { b = 0; } public Example(int y) { super(y + 2); b = 1; } }
Example foo = new Example(); IO.println(foo.a); IO.println(foo.b); Example bar = new Example(8); IO.println(bar.a); IO.println(bar.b);
Consider the following two class definitions.
class X { public double g(double x) { return 3.0 * f(x); } public double f(double x) { return x; } }class Y extends X { public double f(double x) { return 5.0 * x; } }
Y y = new Y(); IO.println(y.f(2.0)); X x = y; IO.println(x.f(2.0)); IO.println(x.g(2.0));
import csbsju.cs160.*;
public class Registrar {
public static void run() {
SemesterEnrollment data = new SemesterEnrollment(100);
while(true) {
IO.print("What to do? ");
String op = IO.readString();
if(op.equals("add")) {
String who = IO.readString();
int how_many = IO.readInt();
data.enroll(who, how_many);
} else if(op.equals("query")) {
String who = IO.readString();
IO.println(data.getCreditsTaken(who));
}
}
}
}
import csbsju.cs160.*;
public class Example {
public static void main(String[] args) {
try {
IO.print("Type something. ");
Integer.parseInt(IO.readString());
IO.println("good");
} catch(NumberFormatException e) {
IO.println("bad");
}
}
}
8 0 14 1
10.0 10.0 30.0