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.
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?
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.
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
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()
.
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.