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
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
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
-
To use the keyboard for input:
- Import
java.util.Scanner
. - Create an input object using the following statement:
Scanner scanner = new Scanner(System.in);
. - To read an integer from the keyboard use the
nextInt()
method of thescanner
object:int integer = scanner.nextInt();
. - In a similar fashion use
nextFloat()
andnextDouble()
for readingfloat
anddouble
numbers, respectively. ForString
input use thenext()
method. - When you finish reading all your input make sure you close the input stream using
scanner.close()
.
- Import
-
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. -
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 aString
that includes spaces? - Replace the
next()
method withnextLine()
then run the application and give it your full name separated with spaces. What was the output? - What is your conclusion?
Solution
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
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
-
Create a project then create a class called
NumbersTest
that contains amain
method. -
Perform the following in the
main()
method:- Define two
double
variables,var1
andvar2
, and initialize them with3.33
and6.67
, respectively. - Store the sum of the two numbers in a variable called
sum
. - 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 labelvar1 =
with the value ofvar1
as follows:System.out.println("var1 = " + var1);
where the
+
operator concatenates two strings. - Define two
Solution
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 , and , using Heron’s formula:
Use your program to calculate the area of the following triangle with side lengths of 9.85, 10.5, and 10.0:
Hint: The square root can be computed using Math.sqrt()
.
Solution
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
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();
}
}