Lesson 1: Introduction to Java
Activity 1: Printing the Results of Simple Arithmetic Operations
Solution:
- Create a class named Operations as follows:
public class Operations
{
- Within main(), print a sentence describing the operation on the values you will be performing along with the result:
public static void main(String[] args) {
System.out.println("The sum of 3 + 4 is " + (3 + 4));
System.out.println("The product of 3 + 4 is " + (3 * 4));
}
}
The output will be as follows:
The sum of 3 + 4 is 7
The product of 3 + 4 is 12
Activity 2: Reading Values from the User and Performing Operations Using the Scanner Class.
Solution:
- Right-click the src folder and select New | Class.
- Enter ReadScanner as the class name, and then click OK.
- Import the java.util.Scanner package:
import java.util.Scanner;
- In the main() enter the following:
public class ReadScanner
{
static Scanner sc = new Scanner(System.in);
public static void main(String...