Cornelius Eanes's Programming Portal
Assignments (User Input)
[19] Asking Questions

Goal: Discover how to grab user input.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Asking Questions
/// File Name: AskingQuestions.java
/// Date Finished: Sep. 21, 2015

import java.util.Scanner;

public class AskingQuestions {
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        int age;
        String height;
        double weight;
        
        System.out.print("How old are you? ");
        age = scanner.nextInt();
        
        System.out.print("How tall are you? ");
        height = scanner.next();
        
        System.out.print("How much do you weigh? ");
        weight = scanner.nextDouble();
        
        System.out.println("So you're " + age + " years old, " +
            height + " tall and " + weight + " pounds.");
        
    }
    
}

Output

[20] More Questions

Goal: Experiment more with the Scanner.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: More Questions
/// File Name: MoreQuestions.java
/// Date Finished: Sep. 21, 2015

import java.util.Scanner;

public class MoreQuestions {
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("What city is the capital of France?");
        scanner.next();
        
        System.out.println("What is 6 multiplied by 7?");
        scanner.next();
        
        System.out.println("What is your favorite number between 0.0 and 1.0?");
        scanner.next();
        
        System.out.println("Is there anything else you would like to tell me?");
        scanner.next();
        
    }
    
}

Output

[21] Even Moar Questions

Goal: Experiment even more with the Scanner.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Even Moar Questions
/// File Name: RudeQuestions.java
/// Date Finished: Sep. 21, 2015

import java.util.Scanner;

public class RudeQuestions {
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        String name;
        int age;
        double weight, income;
        
        System.out.println("Hello. What is your name?");
        name = scanner.next();
        
        System.out.println("Hi, " + name + "! How old are you?");
        age = scanner.nextInt();
        
        System.out.println("So you're " + age + ", eh? That's not old at all.");
        System.out.println("How much do you weigh, " + name + "?");
        weight = scanner.nextDouble();
        
        System.out.println(weight + "! Better keep that quiet. Finally, what's your income, " + name + "?");
        income = scanner.nextDouble();
        
        System.out.println("Hopefully, that is $" + income + " per hour and not per year!");
        System.out.println("Well, thanks for answering my rude questions, " + name + ".");
        
    }
    
}

Output

[22] Name, Age, and Salary

Goal: Specify some specific information for the user to provide.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Name, Age, and Salary
/// File Name: NameAgeSalary.java
/// Date Finished: Sep. 21, 2015

import java.util.Scanner;

public class NameAgeSalary {
    
    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        String name;
        int age;
        double salary;
        
        System.out.println("Hello. What is your name?");
        name = scanner.next();
        
        System.out.println("Hi, " + name + "! How old are you?");
        age = scanner.nextInt();
        
        System.out.println("So you're " + age + ", eh? That's not old at all!");
        System.out.println("How much do you make, " + name + "?");
        salary = scanner.nextDouble();
        
        System.out.println("Hopefully, that is $" + salary + " per hour and not per year!");
        
    }
    
}

Output

[23] More User Input

Goal: Create a table with user-specified information.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: More User Input
/// File Name: MoreUserInput.java
/// Date Finished: Sep. 22, 2015

import java.util.Scanner;

public class MoreUserInput {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String firstName, lastName, loginName;
        int grade, studentId;
        double gpa;

        System.out.println("Please input the following information:\n");
        System.out.print("First Name: ");
        firstName = input.next();

        System.out.print("Last Name: ");
        lastName = input.next();

        System.out.print("Grade (9-12): ");
        grade = input.nextInt();

        System.out.print("Student ID Number: ");
        studentId = input.nextInt();

        System.out.print("Login Name: ");
        loginName = input.next();

        System.out.print("GPA (0.0-4.0): ");
        gpa = input.nextDouble();

        System.out.println("\nYour Information:");
        System.out.println("\tLogin: " + loginName);
        System.out.println("\tID: " + studentId);
        System.out.println("\tName: " + lastName + ", " + firstName);
        System.out.println("\tGPA: " + gpa);
        System.out.println("\tGrade: " + grade);

    }

}

Output

[24] Age in 5 Years

Goal: Do some math with the stored user input.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Age in 5 Years
/// File Name: AgeIn5Years.java
/// Date Finished: Sep. 22, 2015

import java.util.Scanner;

public class AgeIn5Years {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String name;
        int age, ageOlder, ageYounger;

        System.out.print("Hello! What is your name? ");
        name = input.next();

        System.out.print("Hello " + name + "! How old are you? ");
        age = input.nextInt();

        ageOlder = age + 5;
        ageYounger = age - 5;

        System.out.println("In 5 years, you'll be " + ageOlder + " years old.");
        System.out.println("And 5 years ago, you were " + ageYounger + " years old. Wow.");

    }

}

Output

[25] A Dumb Calculator

Goal: Create a calculator that only does addition.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: A Dumb Calculator
/// File Name: DumbCalculator.java
/// Date Finished: Sep 22, 2015

import java.util.Scanner;

public class DumbCalculator {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double num1, num2, num3, result;

        System.out.print("What is your first number? ");
        num1 = input.nextDouble();
        System.out.print("What is your second number? ");
        num2 = input.nextDouble();
        System.out.print("What is your third number? ");
        num3 = input.nextDouble();

        result = (num1 + num2 + num3) / 2;
        System.out.println("(" + num1 + " + " + num2 + " + " + num3 + ") / 2 = " + result);

    }

}

Output

[26] BMI Calculator Part 1

Goal: Create a Body Mass Index (BMI) calculator.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: BMI Calculator Part 1
/// File Name: BMICalculator1.java
/// Date Finished: Sep. 22, 2015

import java.util.Scanner;

public class BMICalculator1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        double height, weight, bmiValue;

        System.out.print("Your height in meters: ");
        height = input.nextDouble();

        System.out.print("Your weight in kilograms: ");
        weight = input.nextDouble();

        bmiValue = weight / Math.pow(height, 2.0d);
        System.out.println("Your BMI is " + bmiValue);

    }

}

Output