Showing posts with label Chpt2. Show all posts
Showing posts with label Chpt2. Show all posts

Saturday, February 13, 2016

Exercise 2.3

QUESTION

  1. Create a new program called Time.java. From now on, I won’t remind you to start with a small, working program, but you should.
  2. Following the example in Section 2.6, create variables named hour, minute and second, and assign them values that are roughly the current time. Use a 24-hour clock, so that at 2pm the value of hour is 14.
  3. Make the program calculate and print the number of seconds since midnight.
  4. Make the program calculate and print the number of seconds remaining in the day.
  5. Make the program calculate and print the percentage of the day that has passed.
  6. Change the values of hour, minute and second to reflect the current time (I assume that some time has elapsed), and check to make sure that the program works correctly with different values.
The point of this exercise is to use some of the arithmetic operations, and to start thinking about compound entities like the time of day that that are represented with multiple values. Also, you might run into problems computing percentages with ints, which is the motivation for floating point numbers in the next chapter.


HINT: you may want to use additional variables to hold values temporarily
during the computation. Variables like this, that are used in a computation
but never printed, are sometimes called intermediate or temporary variables.

SOLUTION

public class ThinkJava2_Ex3 {
public static void main (String[]args) {
//Step 2
int hour, minute, second;
hour = 16;
minute = 24;
second = 39;
//For checking
System.out.println(hour+":"+minute+":"+second);
//Step 3
int secSinceMidNite;
secSinceMidNite = ((hour*60)+minute)*60 + second;
System.out.println("Seconds since midnight = "+secSinceMidNite);
//Step 4
int secRemainingInDay, totalSecInDay;
totalSecInDay = 24*60*60;
secRemainingInDay = totalSecInDay - secSinceMidNite;
System.out.println("Seconds remaining in day = "+secRemainingInDay);
//Step 5
int percentOfDayPassed;
percentOfDayPassed = (secSinceMidNite*100)/totalSecInDay;
System.out.println("Percentage of day passed = " +percentOfDayPassed+"%");
/*Step 6
Change values of hour, minute, second and run again
*/
}
}

Exercise 2.2


QUESTION

  1. Create a new program named Date.java. Copy or type in something like the “Hello, World” program and make sure you can compile and run it.
  2. Following the example in Section 2.4, write a program that creates variables named day, date, month and year. day will contain the day of the week and date will contain the day of the month. What type is each variable? Assign values to those variables that represent today’s date.
  3. Print the value of each variable on a line by itself. This is an intermediate step that is useful for checking that everything is working so far.
  4. Modify the program so that it prints the date in standard American form: Saturday, July 16, 2011.
  5. Modify the program again so that the total output is:
American format:
Saturday, July 16, 2011
European format:
Saturday 16 July, 2011

The point of this exercise is to use string concatenation to display values with different types (int and String), and to practice developing programs gradually by adding a few statements at a time.

SOLUTION

public class ThinkJava2_Ex2 {
public static void main (String [] args){
String day = "Sat";
int date = 2;
String month = "Jan";
int year = 2016;
//Step 3
System.out.println(day);
System.out.println(date);
System.out.println(month);
System.out.println(year);
//Step 4
System.out.println(day+", "+month+" "+date+", "+year);
//Step 5
System.out.println("American format:");
System.out.println(day+", "+month+" "+date+", "+year);
System.out.println("Eurpoean format:");
System.out.println(day+" "+date+" "+month+", "+year);
}
}

Exercise 2.1

QUESTION

If you are using this book in a class, you might enjoy this exercise: find a partner and play “Stump the Chump”:
Start with a program that compiles and runs correctly. One player turns away while the other player adds an error to the program. Then the first player tries to find and fix the error. You get two points if you find the error without compiling the program, one point if you find it using the compiler, and your opponent gets a point if you don’t find it.

SOLUTION

I need a person for this. -_-"
So, no solution.