Review of all commands:
Create variables
- primitive or class (know boolean, double, char, integer, String and what
each holds)
- instance or local
- create using format: type name (a class can be a type)
- create arrays of a type using type[] arrayname = new type[number]
- variables can only have certain identifiers
(no spaces in the name, can't start with a number, no spec characters, etc)
Assign values to variables:
- = or use a method that updates
- identify array elements as arrayname[index]
- get length with arrayname.length
- right side resolves to a value that goes into left side variable name
Compare values:
- >,<,==,=>, =< for primitive types only
- equals or compareTo method for classes
- && means And ; || means Or. Can use parentheses to show precedence.
Decisions
- If ( ) {} else if ( ) {} else {}
- put decisions inside parentheses - resolve to boolean (true or false)
Loops (Repeat)
- For ( starter ; keep going condition; incrementer){}
- While (keep going condition) {}
- Be sure the keep going condition can become false somewhere inside
the loop
- Make sure the loop goes at least once
- Sum construct:
- zero out a variable before the loop (double sum = 0:)
- add to that variable inside the loop (sum = sum + whateverYouWantToCount;)
- print the total after the loop (System.out.println(sum);)
Make methods:
- public [static] returnType methodname (type var, type
var){ }
- return needed if not returnType void
- static means the method cannot use instance variables (cuts off "this")
- input parameters are considered local variables. If they are arrays or objects,
their address will be passed so the parameter will point to the caller's copy
of the variable.
- constructors are special methods with no return type and methodname the
same as the class name
Use methods
- variable = [class or object identifier].methodname(var, var);
- if void type, do not set equal to a variable
- input parameters must match contract
- input parameters should not be created as they are passed (meaning no (int
x, String y), just (x,y))
- if the method is not static, you cannot call it with [class ], you
must use [object identifier].
Make classes:
- name instance variables
- encapsulate with private (fence off anyone of another class from ever using
these directly)
- create constructors: public methodname (type var, type
var){ }
- use instance variables as this.variable name
- methods using this are not static
Use classes
- Scanner, Point, DecimalFormatter, Random, classes you create
- Create instances using constructor:
- classname variable = new classname(var, var);
- Call methods of an instance:
- variable = object identifier.methodname(var, var);
Print to the screen:
- System.out.println ( what you want to output ); // inside parentheses put
"string" and variables separated with +
- System.out.print // does not go to the next line
- System.out.printf (what you want to output, variable names) (This
wont be on the test)
- Can use DecimalFormatter to format inside println and print (Need to read
this, not to create it)
- When printing an object, it will use the object's toString method to format
the object's information
Comments:
- // -> comment line ex: // this is a comment
- /* xxx */ comment between marks
Read from screen:
- Print out to screen first
- Use scanner to read using next(), nextLine(), nextInt(), nextDouble()
- Scanner xxx = new Scanner(System.in);
- int value1 = keyb.nextInt();
Syntax rules
- Every line ends with { or ;
- Only the following that we know end with {: if, else, for, while, method
header, class header
- Inside parentheses, separate with comma only, except for the for statement,
which separates with semi-colon
- Literals inside double quotes
- char inside single quotes
Tracing
- debug: step / step into and see values
- hand tracing: local variables and values inside a class
- follow code that creates objects and calls methods on the objects;
- -----> following them all the way into the class methods
Details on some classes we used:
Scanner: import java.util.*;
create instance: Scanner x = new Scanner(System.in);
call methods: int y = x.next();
Random: import java.util.Random;
create instance: Random x = new Random();
call methods: int y = x.nextInt(6);
Decimal Format: Library: import java.text.DecimalFormat; ---> (we did not
use this much)
Create instance: DecimalFormat percentage2 = new DecimalFormat(0.00%);
Call methods: System.out.println(percentage2.format(.308)); // will print 30.80
0 = required position
# = show it if you have it
% = make it a % (mult by 100)
E = E notation (scientific)
String:
- stringvar.charAt(3) // will be a character
- stringvar.length() // will be an integer
- stringvar.equals(somethingOtherString) // will be either true or false (a
boolean)
- stringvar.toUpperCase() // will be a String that is all uppercase
- stringvar.toLowerCase() // will be a String that is all lowercase
- stringvar.subString(2) // will be a String starting in position 2 to the
end
- stringvar.subString(2, 5) // will be a String starting in position 2 and
ending in position 4
- stringvar.indexOf("xx",2) // return the index of the first place
xx is found, starting the search in position #2.
- Format
- ex: newString = myOriginalString.toLowerCase();
Graphics:
Picture It
import picture knowledge: javalib.worldimages.*;
import color knowledge: java.awt.Color;
Make WorldImages using AImage.make<shape> , AImage.makeFromFile
Place images on top of each other using <your world image>.
place (<your other world image> , x,y)
x indicates how far to the right; y indicates how far down (positive)
Show your image with <your world image>.show()
Exercises to help you review:
1) Create a program with vara = 3 and varb = 2. Swap the values and print "vara
now has " + vara + "varb now has" + varb.
2) In your game:
- Count the number of turns to win
- Determine the average roll
- print a message based on the count:
- print "quick" if count is less than 5
- print "good" if count is less than 10 or exactly 25
- print "long game" if count is greater than or equal to 10,
but not 25
- ask if the user wants to play a new game when they are done
3) Practice exercise:
- Create a class of bikes with a color and number of speeds. Create a constructor
and a method to update each variable and another to give back each variable.
- Create another program to use the bikes that does the following in order:
- Ask the user for the color of 2 bikes, and create those 2 bikes, both with
a speed of 3.
- Add 1 to the number of speeds of the first bike and print the new speed
6 times and print the total of all the printed speeds.
- Add 3 to the number of speeds of the second bike continuously until the
number of speeds exceeds 20, and print the count of how many times the speed
changed.
- Print the color and speed of each bike (by asking the bikes).
- Multiply the speed of each bike by $10 to determine the selling price. Add
a 5% tax. Print the final price, the tax rate, and the tax amount.
- If the final price is equal to $70 or if it is less than $25, print "low";
if it is less than $45 print "ok"; if it is anything else, print
"bad".