James Liang - ICS Blog

ICS4U Blogging

Intro to Java - Multiplayer Coding

Multiplayer Coding

Team/Group:

Create a program that asks the user for their name and year of birth. The program will output:

  1. Their name in all uppercase letters
  2. Their age (based on their year of birth)
  3. The grade they would be in (based on their age)The only exception is if they are named “Trump”. If they are named “Trump” then they should be in kindergarten.
//import Java package to get inputs
import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    //create Scanner object (input) to get inputs
    Scanner input = new Scanner(System.in);
    //declare variables
    int yearBirth;
    int age;
    String name;
    int grade; 
    
    //read user input for name
    System.out.println("What is your last name");
    name = input.nextLine().toUpperCase(); //convert input into upper case

    //read user input and calculate age
    System.out.println("What year were you born?");
    yearBirth = input.nextInt();
    age = 2021 - yearBirth;

    //output name in all uppercase letters
    System.out.println("Hi "+ name);

    //output age
    System.out.println("You are " + age + " years old.");

    //compares two strings, if not true, continues to only one of the else if statements below
    if (name.equalsIgnoreCase("trump"))
    {
      System.out.println("You are in Kindergarten");
    }
    else if (age < 0)
    {
      System.out.println("Are you sure you exist?");
    }
    else if (age > 18)
    {
      System.out.println("You are too old to be in school");
    }
    else if (age <= 5)
    {
      System.out.println("You are too young to be in school or are in kingergarten");
    }   
    else
    {
      grade = age-5;
      System.out.println("And you are in grade " + grade);
    }
    
  }
}

Explanation

In order to scan for user input, we first needed to import the Java scanner package. We kept getting an error with the scanner, and later figured out that we didn’t import it. The main code in this program is located in the Main class. We create a new scanner object (input) to obtain input from the system. After that line, we declare all of the variables that will be used later on. We decided on reading the String before the number as we didn’t want to skip the input of the name. It could’ve been easily fixed, but we wanted to not fix problems that didn’t need to be created. The system then prints out some text with println to inform the user what we want them to input. The next line reads the input and converts it to upper case with string manipulation. The reading of the next input is very similar to the String except we used nextInt() instead since we wanted the Primitive int data type instead of the Non-Primitive String data type. With the int input, we calculate their age by subtracting their birth year to the current year and setting that value as the age. This assumes that their birthday has already passed this year. After this are the outputs using the variables from before and math operators. Since one of the requirements of the program was to check for an exception, we decided to use an if statement with else if and else statements for the final output. The if statement takes the name variable from before and removes the case. We had lots of issues trying to do this if statement and this one worked. If the name matches “trump”, we give the the kindergarten output. After this are some more else if statements, the first checking if the age is below 0, as someone cannot be less than 0 years old. We assumed that anyone who is older 18 is no longer in school and provided an output stating that. The third else if statement checks if the age is less than or equal to 5, we assumed that anyone who fit this criteria would either be not in school or in kindergarten. Finally, if none of the previous statements matched the age, we calculated the grade assuming that the grade of someone is their age subtracted by five years. It then outputs the text along with a math operator to include the grade with the output.