Compiling & Variables 


Variables, Arithmetic Operators, and Expressions

VariableExample.java

ExpressionExample.java

More about % mod operator.

Deitel Chapter 2


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 VarEx.  Except for the package statement, replace all of Netbeans' autogenerated code with the following code example.

This code is riddled with problems, some are errors, others just don't make sense.  Fix it to make the most sense based on the comments. There could be more than one thing wrong in a given line.

Note that what is wrong is NOT that these don't happen to be your name or your age or your income.  We're allowed to write programs that aren't about ourselves.  I want you to show that you can fix this program's syntax so it works for the values given.

public class Varex {
	public static void main(String[] args) {
		// my income is $4491.98
		double income = "44991.98"; }
		// my income increased by $100
		income = 100;
		// my age is 21
		String age : 21;
		// my age doubled 
		age * 2;
		// my name is Cuthbert
		name= "Cuthbert";
		// print out my income, age, and name
		System.out.println("Your income is " income);
		System.out.println("Your age is: age")
		System.out.println(Your name is String);
}

Part B

Continuing in the same (fixed) main from above, create variables, using appropriate types and making up appropriate variable names, for the following:

Make up an appropriate value for each of these and assign the variable the value.  You may do this assignment on the same line (in the same statement) as you declared the variable, or in a later statement.

For each of the variables, print out a message, saying what the variable represents and the current value of the variable.  For example, the first line of output might be

First puppy name: Mr. Fuzzbudget

For one of the variables, add another line of code that changes its value, and then print another message, saying what it was changed to.

protip: Run frequently as you go, it's a fast way to check you're on track. 

protip: It's always okay to add extra output statements to check the current values of your variables as you work.  Comment these out or delete them when you don't need them anymore.


Part C

You have two variables for the names of two puppies, and you have printed them.  Now write code that swaps the contents of these variables and then print them again. 

This usually sounds easy at first, so consider:
// starting values
int x = 3;
int y = 4;
// now swap them?
x = 4; 
y = 3; 
// WRONG! the program did not swap their contents!!!
// this is the PROGRAMMER doing the work of remembering the two values and assigning them again
// we want JAVA to do the work, so that it works no matter what the current values of x and y are
// if we went back and changed the first line to int x = 5; then the "swap" wouldn't work anymore
// Hint: Does this do what we want??  TRY IT!
x = y;
y = x;
Big hint: suppose I have a cup full of milk and a cup full of coke and I want to swap them. If I try to do this by pouring the milk into the coke cup and the coke into the milk cup... does that work?  No.  I would need... a third cup.  Then I could pour the milk into the empty third  cup, then the coke into the old milk cup (now empty), and then the milk (from the extra cup), into the old coke cup.  So if I'm swapping values from two variables, I need ... a third variable, to hold one of the values temporarily.