The main goals for this lab are for you to get more comfortable with variables, reading in data, and connect to the lab machines. We will be covering the following topics:
String
, double
, int
System.out.println()
, System.out.print()
System.console().readLine()
for reading in data+
, *
, -
, \
, %
+
This lab is a paired programming assignment. What exactly does that mean? You will be working in pairs on the CS lab computers. Each pair will be working on one computer. One person will be the driver and the other person will be the navigator. Here is the rule: the driver controlls the lab computer, but the driver can only type what the navigator tells them to type. For this to work well, each pair should be constantly talking among themselves. After each problem, you will switch roles, the navigator will become the driver and the driver will become the navigator.
Before leaving the lab, make sure you go through your answers with a TA or instructor.
Hopefully you completed Lab00 so far. If not, then run the following commands on the terminal on the lab machine:
mkdir cs113
cd cs113
mkdir labs
cd labs
mkdir lab01
cd lab01
To confirm this works, you should see something like
edinella@goldengate:~/cs113/labs/lab01$
on the command prompt.
You will hopefully see your username instead of edinella
and different
name after the @
symbol besides for goldengate
. Lab00 explains what the second
name means.
Another way to confirm you followed the steps above correctly is to run
pwd
. You should the see something like /home/edinella/cs113/labs/lab01
(where
again it will say your username rather than edinella
.
Each row in the table below represents an expression. Please fill in the value and types for each expression.
Expression | Value | Type |
---|---|---|
2+3 | ||
2.0 + 3 | ||
5.5 / 10 | ||
5 - 2 * 3 | ||
"123" | ||
48 % 2 | ||
55 % 4 | ||
2+4*2+1 | ||
“I” + “love” + “CS113!” |
Note: The next few problems will require asking the user for information, storing the information, and then doing something with it. Look at slide 6 from the 2nd lecture on how to read in what the user writes in the console.
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
Modify your Sum program to match the following format. That is, the user input should be on the same line as the prompt.
$ 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
TODO: Question 1.1: Run the Sum1
program but now input a number with a decimal point. What happens, and why?
Write a program Sum3.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 exmple.
$ javac Sum3.java
$ java Sum3
Please give me one number: 5.5
Please give me a second number: 2.6
The sum of the two numbers is 8.1
Checkpoint: Before continuing, make sure a TA or instructor has checked your code and answers to the previous part.
Write a madlib program called Roses
that asks the user for 2 colors and one adjective, then prints out the filled-in poem. If you are not familiar with madlibs, its the worlds greatest game, at least thats what they say on their website. Take a quick break and play one with your partner, you’ll have fun: https://www.madtakes.com/
Below is an example of compiling and 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!
Write a program called Bill
that takes a user’s hotel room rate and outputs the cost of a 6% sales tax and 10.5% city tourism fee. The program should then output the total bill. Below is an example
$ javac Bill.java
$ java Bill
Room rate: 100
Total nights: 3
-------------------
Tax: $ 18.0
Tourism Fee: $ 31.5
Total: $ 349.50
Checkpoint: Before continuing, make sure a TA or instructor has checked your code and answers to the previous part.
For the rest of the lab, you will be reading and analyzing code.
Read the following program called Cats
. What would the output of this program be?
public class Cats {
public static void main(String[] args) {
String type = "tuxedo";
int fish = 3;
System.out.println("The "+type+" cat caught "+fish+" fish.");
}
}
The following programs have a problem. For each program, what is the error? Is error is syntax error, runtime error, or logic error? How can we fix the problem?
syntax
errors are issues that a compiler will catch that will prevent the program from compilingruntime
errors are issues that cause the program to crash when the program is running.logic
errors are issues in the algorithm. For example, say we incorrectly add a 7% sales tax rather than a 6% sales tax above in the Bill
program.public class Mystery1 {
public static void main(String[] args) {
String input = System.console().readLine()
int value = Integer.parseInt(input);
double fraction = value / 10
System.out.println("The fraction is "+fraction);
}
}
TODO: Quesiton 5.2.1.
what is the error? Is the error is syntax error, runtime error, or logic error? How can we fix the problem?
public class Mystery2 {
public static void main(String[] args) {
String input = System.console().readLine();
int value = Integer.parseInt(input);
double fraction = value / 10;
System.out.println("The fraction is "+fraction);
}
}
TODO: Quesiton 5.2.2.
what is the error? Is the error is syntax error, runtime error, or logic error? How can we fix the problem?
public class Mystery3 {
public static void main(String[] args) {
String input = System.console().readLine();
int value = Integer.parseInt(input);
double fraction = value / 0;
System.out.println("The fraction is "+fraction);
}
}
TODO: Quesiton 5.2.3.
what is the error? Is the error is syntax error, runtime error, or logic error? How can we fix the problem?
In todays lab we covered variables, reading input from a user, and errors.
Before leaving, make sure your TA/instructor have signed you out of the lab. If you finish the lab early, you are free to go. If you did not finish, make sure you attend office hours where a TA will sign off that you completed the lab.