Variables and Expressions


VariableExample.java

ExpressionExample.java

More about % mod operator.

Deitel chapter 2, 3


Classwork

Note: when printing numbers, I do not care about beautiful formatting.  If you are printing fifteen decimal places, that's fine.  If you really,really,really care, you can look up printf in the text and use that to format your output beautifully.


Part A
Create a project and in it a class with a main and in the main (this always means inside the curly braces of the main method), add three integer variables, h1, h2, and h3, representing homework grades in a course.  Make up values for each.

Add a new double variable hAvg, and set it to the average of these three grades. Print the values and the result to make sure it works.  (hint: if you are getting a whole number for your average, try adjusting your grades up or down slightly.  Are you still getting a whole number?  Why might you get a whole number when you shouldn't?)

Your output should make it clear what you are printing, but you don't have to be fancy or worry about number formatting.

Do the same for c1, c2, c3, and cAvg, representing classwork grades and their average.

After this, swap the contents of the variables c1 and c2 (don't just re-assign the values, write java code that will swap whatever values they currently contain).  Hint: if you have a cup of coke and a cup of milk, can you swap them without a third, empty cup?  So what do you need to be able to do this swap?  Print again to show you have swapped successfully

Add a new integer variable tg, representing a test grade.  We will read this in from the user.

We need to add some code to be able to read from the user. At the top of your program, before the class (but after the package statement), paste

import java.util.Scanner;
As the first line inside your main method, paste
Scanner scan = new Scanner(System.in);
In order to read an int from the user, we use the nextInt() method like this
int x;
x = scan.nextInt();

(hint: that example used a variable named x.  This DOES NOT MEAN you must always use x as your variable name for this to work. )

Note that on the Class Page there is a link to a page with scanner example code so you can refer to this whenever you need to read in from the user for an assignment.

Have the program ask (by printing), what your test score is, and then use scan.nextInt() to read in the answer and store it in tg.

Add a new double variable grade, representing the overall class grade.  A weighted average is found by multiplying the average for each component by the weight for that component and then adding up the results for all components.  Based on a course with 50% classwork, 40% homework, 10% test, set grade to the weighted average based on hAvg, cAvg, and tg, and print this final grade.


Part B

Short answer (in a Word document, attached to your submission, not hidden inside your project directory): without running it, what will the value of z be at the end of the following code? Show step-by-step how the expression would be evaluated by Java.  (Hint: copy each line to the next, then make the next change.) My solution has 10 steps.

 
int x = 42;
String y = "7";
String z = y + 6 * 6 + x / 2 + x + "y";

 


Part C [EC]

To your project, in the same package, add another class with a main (you can just copy the declaration of the main method from the other file). 

In the main, ask the user for a 5 digit number and read it in as an int.  (you don't have to worry about the case where they give bad input, your code only has to work for 5 digit integers)

Separate this number into individual digits and print them once per line.  So if we were given 12345 we would print

1
2
3
4
5

Don't use anything we haven't talked about so far in the course to do this.