Lab 2: Variables, Converting Data Types, Input / Output
Objectives
-
The main goals for this lab are:
-
Variables: defining and assigning
-
Data types: String, double, int
-
System.out.println(), System.out.print()
-
System.console().readLine() for reading in data
-
arithmetic operators: +, *, -, \, %
-
String concatenation with
-
Operators and expressions, order of operations
Make sure to write down your answers for each Question below. Whenever you see TODO, that means there is an action item for you to complete. When you see Question, that means you should fill in your answer on the worksheet provided to you. When you believe you have finished the lab, show Prof. Shiptoski your work.
0. CS Lab Machine Directory Setup
In the first lab, you should have set up your /lab01 directory.
On your lab machine, open the terminal, and run the following commands.
cd /home/your-username/cs113/labs/lab01
pwd
You should see this after running pwd: /home/your-username/cs113/labs/01
Great, we have verified you have a /lab01 directory, and set your current working
directory to your /lab01 directory.
1. Expressions
For each expression, write down the value of the expression, and the date type of the expression.
1: 2 + 3
Answer:
2: 2.0 + 3
Answer:
3: 5.5 / 10
Answer:
4: 5 - 2 * 3
Answer:
5: "123"
Answer:
6: 48 % 2
Answer:
7: 55 % 4
Answer:
8: 2 + 4 * 2 + 1
Answer:
8: "i" + "love" + "CS113!"
Answer:
2. Sum.java
Sum1
Write a program Sum1.java that asks the user for two integers and then prints the sum of the numbers. You can assume that the user will put in valid integers. When run, your program should do the following: 1) the program asks a user for a number 2) the user enters an integer 3) the program asks a user for a second number 4) the user enters an integer 5) the program prints the sum. See if you can exactly match the format below!
$ javac Sum1.java
$ java Sum1
Please give me one number:
5
Please give me a second number:
2
The sum of the two numbers is 7
Checkpoint: Before continuing, have Prof. Shiptoski check your work.
Sum2
Write a program Sum2.java that asks the user for two numbers and then prints the sum of the numbers. This time, the numbers should be able to contain decimal points. Below is an example.
$ javac Sum2.java
$ java Sum2
Please give me one number:
5.5
Please give me a second number:
2.6
The sum of the two numbers is 8.1
Sum3
Write a program named Sum3.java that takes in two numbers on the command line and then prints the sum of the two numbers. The numbers can contain decimals.
$ javac Sum3.java
$ java Sum3 5.5 2.6
The sum of the two numbers is 8.1
Checkpoint: Before continuing, have Prof. Shiptoski check your work.
3. Madlibs
Write a madlib program called Roses that asks the user for 2 colors and one adjective, then prints out the filled-in poem.
Below is an example of running the program.
$ javac Roses.java
$ java Roses
color: pink
color: azure
adjective: funny
Roses are pink
Violets are azure
Sugar is funny
and so are you!
TODO: This is the end of the second lab, please show Prof. Shiptoski your Madlibs code and hand in your completed worksheet.