1 Initialization

Initialization

Objective

  • Create a new Java project, package, and class using an IDE.
  • Edit, correct, and run a simple Java program within an IDE.

Exercise 1

App.java
public class App {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Change the above program so that it outputs your name repeated 10 times, each on a new line. Hint: use a for loop.

Solution
App.java
public class App {
  public static void main(String[] args) {
    for (int index = 0; index < 10; index += 1) {
      System.out.println("Dane Doe");
    }
  }
}

Exercise 2

Keyboard Input

  1. To use the keyboard for input:

    1. Import java.util.Scanner.
    2. Create an input object using the following statement: Scanner scanner = new Scanner(System.in);.
    3. To read an integer from the keyboard use the nextInt() method of the scanner object: int integer = scanner.nextInt();.
    4. In a similar fashion use nextFloat() and nextDouble() for reading float and double numbers, respectively. For String input use the next() method.
    5. When you finish reading all your input make sure you close the input stream using scanner.close().
  2. Write an application that reads two strings representing the user’s first name and last name and then concatenates the two strings in a new string called fullName. Insert a single space to separate the first name and the last name. Display the full name.

    Hint: use the next() method of the input object as described above.

  3. Now, run the application and enter your name, your middle name, and your last name all in one line.

    • What was the output?
    • Can the next() method read a String that includes spaces?
    • Replace the next() method with nextLine() then run the application and give it your full name separated with spaces. What was the output?
    • What is your conclusion?
Solution
Input.java
import java.util.Scanner;
 
public class Input {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
 
    System.out.print("First name: ");
    String firstName = scanner.nextLine();
 
    System.out.print("Last name: ");
    String lastName = scanner.nextLine();
 
    System.out.println(firstName + " " + lastName);
 
    scanner.close();
  }
}

Exercise 3

Write an application that reads three integer numbers. Calculate the sum and average for these numbers and display the three numbers and their sum and average.

Solution
SumAverage.java
import java.util.Scanner;
 
public class SumAverage {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
 
    System.out.print("First number: ");
    int firstNumber = scanner.nextInt();
 
    System.out.print("Second number: ");
    int secondNumber = scanner.nextInt();
 
    System.out.print("Third number: ");
    int thirdNumber = scanner.nextInt();
 
    int sum = firstNumber + secondNumber + thirdNumber;
    double average = sum / 3.0;
 
    System.out.println("Sum: " + sum);
    System.out.println("Average: " + average);
 
    scanner.close();
  }
}

Exercise 4+

Concatenating Text and Numbers

  1. Create a project then create a class called NumbersTest that contains a main method.

  2. Perform the following in the main() method:

    1. Define two double variables, var1 and var2, and initialize them with 3.33 and 6.67, respectively.
    2. Store the sum of the two numbers in a variable called sum.
    3. Output the values of the two variables and the sum, properly labelled, each on a separate line.

    Hint: To output var1 = 3.3, we concatenate (join) the label var1 = with the value of var1 as follows:

    System.out.println("var1 = " + var1);

    where the + operator concatenates two strings.

Solution
NumbersTest.java
public class NumbersTest {
  public static void main(String[] args) {
    double var1 = 3.33;
    double var2 = 6.67;
    double sum = var1 + var2;
 
    System.out.println("var1: " + var1);
    System.out.println("var2: " + var2);
    System.out.println("sum: " + sum);
  }
}

Exercise 5+

Write an application for the calculation of the area of a scalene triangle whose side lengths are aa, bb and cc, using Heron’s formula:

s=12(a+b+c)area=s(sa)(sb)(sc)\begin{align*} s &= \tfrac{1}{2}(a+b+c) \\ \text{area} &= \sqrt{s(s-a)(s-b)(s-c)} \end{align*}

Use your program to calculate the area of the following triangle with side lengths of 9.85, 10.5, and 10.0:

10.09.8510.5ABC

Hint: The square root can be computed using Math.sqrt().

Solution
ScaleneArea.java
import java.util.Scanner;
 
public class ScaleneArea {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
 
    System.out.print("Side a: ");
    double a = scanner.nextDouble();
 
    System.out.print("Side b: ");
    double b = scanner.nextDouble();
 
    System.out.print("Side c: ");
    double c = scanner.nextDouble();
 
    double s = (a + b + c) / 2;
    double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
 
    System.out.println("Area: " + area);
 
    scanner.close();
  }
}

Exercise 6+

Write an application to read three numbers representing the percent scores of a student in three subjects: Math, Computer, and English, and then compute and display their average. If the average is less than 60 then print an asterisk, i.e. a * next to the average.

Test your program once with a set of inputs that produces an average greater than 60 and once with a set of inputs that produces an average that is less than 60.

Solution
GradeAverage.java
import java.util.Scanner;
 
public class GradeAverage {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
 
    System.out.print("Math score: ");
    double mathScore = scanner.nextDouble();
 
    System.out.print("Computer score: ");
    double computerScore = scanner.nextDouble();
 
    System.out.print("English score: ");
    double englishScore = scanner.nextDouble();
 
    double average = (mathScore + computerScore + englishScore) / 3;
 
    System.out.print("The average score is: " + average);
    if (average < 60) {
      System.out.print(" *");
    }
    System.out.println();
 
    scanner.close();
  }
}