My teacher gave me this question:
Write a program that does the following:
- Enter your first name: Peter
- Enter your last name: Opunga
- Enter your birth year: 1992
- Hi Peter Opunga, you will be 20 by the end of this year.
- Bonus 1: Proper commenting. (10%)
- Bonus 2: Create only 2 Strings in the entire program. (10%)
- Bonus 3: Use exactly 4 System.out.print. (10%)
Now I am completely new to Java. I have just been introduced to it a little over a week ago and this is what I came up with:
import java.io.*;
public class A1Chuah {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
//Prints instructions for user to enter first name.
System.out.println("Enter first name: ");
//Obtains user input for first name.
String first = in.readLine ();
//Prints instructions for user to enter last name.
System.out.print ("Enter last name: ");
//Obtains user input for last name.
String last = in.readLine();
//Prints instructions for user to enter birth year.
System.out.print ("Enter your birth year: ");
//Obtains user input for birth year.
String year = in.readLine ();
//Converts String year to int useryr
int useryr = Integer.parseInt(year);
//Sets int oriyr to value of 2012
int oriyr = 2012;
//Sets int outyr as oriyr - useryr
int outyr = oriyr - useryr;
//Prints out information.
System.out.println("Hi" + " " + " " + last + "," + " " + "you will be" + " " + outyr + " " + "by the end of this year");
I've managed to complete Bonus 1 and 3 but can't seem to figure out Bonus 2. Please help! P.S. He said that I can get help as long as I don't try to pass it off as my own idea.
You can use one string to get the full name and one for the year.. you can do it this way
System.out.println("Enter first name: ");
String name = in.readLine();
System.out.print ("Enter last name: ");
name+= " " + in.readLine();
System.out.println(name); // should print first and last