CS 163/164, Fall 2017
Lab 4 - Scanners and Expressions
Thursday, Aug. 31st or
Friday, Sep. 1st
Objectives of this Lab
- Update your eid in zyBooks
- Learn how to interpret Java expressions,
- and create and use a Scanner to read keyboard input,
- and practice using System.out.printf to format output,
- and implement the quadratic formula in Java.
Update your eid in zyBooks
In zyBooks, it will ask for your student ID. Make this your eid NOT your 9-number student ID number.
Your eid is your username on the linux systems.
Take a moment to log into zyBooks and update this now.
Create a new Java Project named R4, and make a class named R4. Add a comment
that shows what the following Java code prints. You must take into account integer
versus floating-point math, operator precedence, and parentheses. Your TA
will show you a chart of operator precedence.
// Operator precedence, integer math
System.out.println(2 - 23 % 10 + 3 * 12 - 20);
// Operator precedence, floating-point math
System.out.println(2.4 + 23.1 - 10.0 * 3.1 - 12.5 / 6.25);
// Operator precedence, mixed math
System.out.println(65 % 15 + 3.5 * (5 / 2));
After completing the comments, copy the code into the main method in R4.java
and see how well you can interpret Java. Your TA will discuss the answers to
the questions, before starting on the programming exercise below.
Your TA will discuss the Scanner class:
- Declaring and initializing Scanner objects
- nextInt() - reads an integer
- nextDouble() - reads a double
- next() - reads a token
- nextLine() - reads a line of text
- hasNext() - checks if there is another token
- close() - closes scanner
Comment out the code that you copied from above, and add new code as shown below.
Following the instructions below, in order.
- Declare three variables of type int called A, B, and C, which represent
the quadratic equation coefficients.
- Declare two variables of type double called positiveRoot and negativeRoot,
to store the roots of the quadratic equation.
- Declare and instantiate an object of type Scanner to read from the keyboard,
using a name of your choice.
- Prompt the user to input the A coefficient, using System.out.print, as follows:
"Enter coefficient A: ".
- Read an integer from the keyboard into the variable A.
- Prompt the user to input the B coefficient, using System.out.print, as follows:
"Enter coefficient B: ".
- Read an integer from the keyboard into the variable B.
- Prompt the user to input the C coefficient, using System.out.print, as follows:
"Enter coefficient C: ".
- Read an integer from the keyboard into the variable C.
- Print the quadratic equation in the format shown in the sample output below.
- Look up the quadratic formula on the web.
- Compute the positive root and store it into the variable positiveRoot.
- Compute the negative root and store it into the variable negativeRoot.
- Print the positive root, with one digit after the decimal point, as shown below.
- Print the negative root, with one digit after the decimal point, as shown below.
Enter coefficient A: 1
Enter coefficient B: -11
Enter coefficient C: 24
Formula: 1x^2 + -11x + 24
Positive root: 8.0
Negative root: 3.0
- Make sure your R4.java compiles and produces 6 lines of output.
Have your TA review your output for credit for this recitation