CS 145 Lecture 5 – Problems
Agenda
- what ?s
- quiz in second half of lab on Monday
- 15 multiple choice, fill-in-the-blank
- based on readings, lectures
- what type is this?
- program this
TODO
- Read chapter 2 through section 2.2.
- Write two or more questions that might be asked on the quiz on a 1/4 sheet.
What Type is This?
- Average number of children
- A month
- An answer to a multiple choice question
- Sex
- Gender
- Name
- The number of seconds since January 1, 1970
- A 1-to-5 star rating
- A phone number
- A letter grade
Program This
Pick one of the following to solve. Declare variables, craft expressions, and print out the requested information.
- You are given
n
pictures, all square-shaped and of the same size. How can you lay them out so that they are as many rows as there are columns? - Your kid brother has invented a guess-a-number game. You say the number, and he tells you how far away you are from the right number. For instance, suppose the number is 9. You guess 11. He says “2 away!” You guess 8, he says “1 away!”. You guess 7, he says “2 away!” Play one guess-response exchange.
- On day 0, 1 person gets a terrible disease. On day 1, this person infects 10 others. On day 2, these 10 people each infect 10 others, for a total of 100 new infections. On day three, the 100 each infect 10 others, for a total of 1000 new infections. How many days pass before half the US population (313.9 million) is infected?
- A knight is on a toroidal chessboard. That is, if it moves off the edge of the board, it comes back in on the opposite side. Suppose the knight is at position (c, r). What is its new position if it moves from the top of an L to its bottom-right?
Code
Projectile.java
package lecture0912;
public class Projectile {
public static void main(String[] args) {
final double acceleration = -4.9;
// acceleration = 8;
final double initialVelocityY = 30.0;
final double initialVelocityX = 10.0;
final double initialHeight = 10.0;
double time;
double positionY;
double positionX;
time = 0.0;
positionY = acceleration * time * time +
initialVelocityY * time +
initialHeight;
positionX = initialVelocityX * time;
System.out.println("(" + positionX + "," + positionY + "),");
time = 1.0;
positionY = acceleration * time * time +
initialVelocityY * time +
initialHeight;
positionX = initialVelocityX * time;
System.out.println("(" + positionX + "," + positionY + "),");
time = 2.0;
positionY = acceleration * time * time +
initialVelocityY * time +
initialHeight;
positionX = initialVelocityX * time;
System.out.println("(" + positionX + "," + positionY + "),");
time = 3.0;
positionY = acceleration * time * time +
initialVelocityY * time +
initialHeight;
positionX = initialVelocityX * time;
System.out.println("(" + positionX + "," + positionY + "),");
time = 4.0;
positionY = acceleration * time * time +
initialVelocityY * time +
initialHeight;
positionX = initialVelocityX * time;
System.out.println("(" + positionX + "," + positionY + "),");
}
}
Pictures.java
package lecture0912;
public class Pictures {
public static void main(String[] args) {
int n = 64;
System.out.println(Math.sqrt(n));
}
}
GuessANumber.java
package lecture0912;
public class GuessANumber {
public static void main(String[] args) {
int n = 5;
int guess = 23;
System.out.println(Math.abs(guess - n));
}
}
Disease.java
package lecture0912;
public class Disease {
public static void main(String[] args) {
int population = 313900000;
System.out.println(Math.log10(population));
}
}
Knight.java
package lecture0912;
public class Knight {
public static void main(String[] args) {
int c = 6;
int r = 7;
int newC = (c + 1) % 8;
int newR = (r + 2) % 8;
System.out.printf("(%d,%d) to (%d,%d)", c, r, newC, newR);
}
}
Haiku
on our feelings about being wrong:
We’re wrong when we code
Wrong as a sculptor is wrong
At a ball of clay
We’re wrong when we code
Wrong as a sculptor is wrong
At a ball of clay