CSCI 160 Problem 1
Example solution
import csbsju.cs160.*;
public class Question1 {
public static void main(String[] args) {
int num_even = 0;
for(int i = 0; i < 5; i++) {
IO.print("Number? ");
int n = IO.readInt();
if(n % 2 == 0) {
num_even++;
}
}
IO.println(num_even);
}
}
Example test input
Number? -6
Number? 148
Number? 0
Number? 26
Number? 40
5
CSCI 160 Problem 2
Example solution
import csbsju.cs160.*;
public class Question2 {
public static void main(String[] args) {
double[] numbers = new double[5];
double total = 0.0;
for(int i = 0; i < numbers.length; i++) {
IO.print("Number? ");
numbers[i] = IO.readDouble();
total = total + numbers[i];
}
IO.println(total);
for(int i = numbers.length - 1; i >= 0; i--) {
IO.println(numbers[i]);
}
}
}
Example test input
Number? -3.8
Number? 12e10
Number? -8e10
Number? -4e10
Number? 7.6
3.799996948242187
7.6
-4.0E10
-8.0E10
1.2E11
-3.8
(The close approximation to 3.8 would be fine here; some precision
would be lost when the bigger numbers are added.)
CSCI 160 Problem 3
Example solution
import csbsju.cs160.*;
public class Question3 {
public static void main(String[] args) {
IO.print("Year? ");
int y1 = IO.readInt();
IO.print("Month? ");
int m1 = IO.readInt();
IO.print("Day? ");
int d1 = IO.readInt();
IO.println();
IO.print("Year? ");
int y2 = IO.readInt();
IO.print("Month? ");
int m2 = IO.readInt();
IO.print("Day? ");
int d2 = IO.readInt();
Date start = new Date(y1, m1, d1);
Date curr = new Date(y1, m1, d1);
Date end = new Date(y2, m2, d2);
int count = 0;
while(!curr.equals(end)) {
curr.increment(1);
count++;
}
IO.println(count + " days between " + start.toString()
+ " and " + end.toString());
}
}
Example test input
Year? 2002
Month? 0
Day? 1
Year? 2002
Month? 0
Day? 1
0 days between 1 Jan 2002 and 1 Jan 2002
CSCI 160 Problem 4
Example solution
import csbsju.cs160.*;
public class Rectangle {
private int left;
private int top;
private int width;
private int height;
public Rectangle(int in_left, int in_top, int in_width, int in_height) {
left = in_left;
top = in_top;
width = in_width;
height = in_height;
}
public boolean contains(int x, int y) {
if(x >= left && x <= left + width && y > top &&
y <= top + height) {
return true;
} else {
return false;
}
}
public int getArea() {
return width * height;
}
public void grow(int amount) {
left -= amount;
top -= amount;
width += 2 * amount;
height += 2 * amount;
}
}
Example test input
import csbsju.cs160.*;
public class RectangleTest2 {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(-2, -2, 4, 4);
if(!rect1.contains(2, 2)) IO.println("fails 1");
if(!rect1.contains(-2, 2)) IO.println("fails 2");
if(rect1.contains(3, 0)) IO.println("fails 3");
if(rect1.contains(0, 3)) IO.println("fails 4");
Rectangle rect2 = new Rectangle(10, 10, 30, 30);
rect2.grow(10);
if(!rect2.contains(0, 40)) IO.println("fails 5");
if(rect2.contains(-1, 50)) IO.println("fails 6");
if(rect2.contains(0, 51)) IO.println("fails 7");
if(rect1.contains(3, 3)) IO.println("fails 8");
IO.println("done");
}
}