QUESTION
- 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.
- 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.
- 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.
- Modify the program so that it prints the date in standard American form: Saturday, July 16, 2011.
- Modify the program again so that the total output is:
American format:
Saturday, July 16, 2011
European format:
Saturday 16 July, 2011
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);
}
}
No comments:
Post a Comment