User:ddixon
From ggc
- AssignmentSevenGame
- Derrick/LightsOutModel
- TestingOneTwoThree
- DoItPleaseWork
- AssignmentEightLightsOutGame
- AltAssignmentPurse
Notes
Study Guide - Test 2 1. Declare an array of integers called y.
int[] y;
2. Write a statement to initialize the integer array y to be able to hold 27 elements.
y =new int[27];
3. Write a loop to fill the array y with the even numbers from 20 to 126.
for(int i = 0; i <= 53; i++)
y[i]=20+2*i;
4. Given an array of integers called y, write a loop to print all of the integers to the console on separate lines.
for(int i = 0; i < y.length; i++)
{
System.out.println("The number is " + y[i]);
}
5. Given an array of integers called y, print out all of the odd integers to the console.
for(int i = 0; i < y.length; i++)
{
if(y[i]%2 != 0) // y[i]%2 == 1
{
System.out.println("The number is " + y[i]);
}
}
6. Write a method that changes each element in the array y to triple its original value.
for(int i = 0; i < y.length; i++)
{
y[i]=3*y[i];
}
7. Which type of variable is known about most widely (has the largest scope): instance variable, local variable or parameter? instance variable
8. What is the difference between defining (writing the declaration and body) a method and calling (also known as invoking) a method?
9. When should you use arrays? ArrayLists?
ArrayList<Integer> x;
x=new ArrayList<Integer>();
for(int i=20; i<=126; i+=2)//increase i by 2, i=i+2
{
x.add(i);
}
for(int i=0; i<x.size(); i++)
{
if(x.get(i)%2==1)
{
System.out.println(x.get(i));
}
}
for(int i=20; i<=126; i+=2)//increase i by 2, i=i+2
{
x.add(i);
}
10. What is a plain English description of what the following loop does? for(int i=0; i<y.length; i++) { if(y[i]>0) System.out.println(y[i]); }
10110=1*16 + 0*8 + 1*4 + 1*2 + 0*1 = 16+0+4+2+0=22
10110.11=1*16 + 0*8 + 1*4 +1*2 + 0*1 + 1*1/2 +1*1/4= 22.75
22 % 2 = 0 11 % 2 = 1 5 % 2 = 1 2 % 2 = 0 1 % 2 = 1 0 % 2 = 0 0 % 2 = 0 0 % 2 = 0 00010110 = 10110
0.625 * 2 = 1.25 0.25 * 2 = 0.5 0.5 * 2 = 1.0 0 * 2 = 0.0 0 * 2 = 0.0
0.625=0.101000000
300=300 000300 = 300
variable a storaqge location in the computer's memory that has a type, aname and a contents.
Example: String greeting = "Hello, World!"; greeting is the variable.lowerUpper
method-contains a collection of programming instructions that tell how to carry out a task.
Example: System.out.println("Hello, World!"); println is a method
class-defines the methods that you can apply to its objects
Example: public class HelloPrinter , HelloPrinter is a class UpperUpper
constructor-is a method that initializes a newly instantiated object.
Example: Rectangle box = new Rectangle (5, 10, 20, 30); Constructs a Rectangle
parameter-is an input to a method
Example:System.out.println; (greeting); (greeting) is a parameter
return value-a result that the method has computed for use by the code that called it.
Example: int n=greeting.length(); returns a value
object-a value of a class type
Example: System.out.println; ("Hello, World!"); ("Hello, World!") is an object that belongs to the String class
instance field-variable defined in a class for which every object of the class has it's own value (values are unique to each instance of a class)
Example: balance variable of BankAccount class
local variable-variable whose scope is a block, only visible to methods where the are declared and are not accessible from the rest of the class.
Example: newBalance of the deposit method
implicit parameter-is the object on which the method is invoked
Example: System.out.println; ("Hello, World!"); System.out is the implicit parameter.
explicit parameter-a parameter of a method other than the object on which the method is invoked.
Example: System.out.println; (greeting); (greeting) is an explicit parameter
declare-Specifies name and type
Example: Rectangle rectangle; Where Rectangle in the type and rectangle is the name.
initialize-setting a variable to a well-defined value when it is created.
Example: rectangle = new Rectangle (10, 10, 20, 30); (10, 10, 20, 30) gives height, width,(x and y= location)
type-a named set of values and operations that can be carried out with them.
Example: ("Hello, World!") has the type String.
access specifier- A keyword that indicates the accessibility of a feature.
Example: public class BankAccount public is the access specifier.
"2" a string converted in a string bond from and integer to string 2 is an integer however it can be converted into a string as in a String bond, '2' is a Character constant as seen in appendix B "2.0" a string converted in a string bond from and integer to string 2.0 is an double type integer
public, class, private, import, package keyword
/**
* All about my class here.
* @author Jam Jenkins
*/ comment
public class HelloGame extends GameLoop class declaration
private Sprite hello; instance variable declaration (field declaration)
public void startGame() method declaration
hello=new StringSprite("Hello World"); initialization
hello.setLocation(0.5, 0.5);
and
canvas.addSprite(hello); calling a method (invoking a method)
hello argument
{
private Sprite hello;
public void startGame()
{
hello=new StringSprite("Hello World");
hello.setLocation(0.5,0.5);
canvas.addSprite(hello);
}
} class body
{
hello=new StringSprite("Hello World");
hello.setLocation(0.5,0.5);
canvas.addSprite(hello);
} method bocy
Jam, fang, HelloGame, GameLoop, hello, StringSprite identifier
The for Statement
for (initialization; condition; update)
statement
Purpose: TO execute an initialization, then keep executing a statement ans updating an expression while a condition is true.i=iterations
debugger is a program that you can use to execute another program and analyze its run-time behavior Array
1.an Array is a sequence of values of the same type 2. how to construct an Array with 10 floating point number: new double [10] 3. declarion of an array variable: double[] data = new double[10] That is, data is a ference to an array
4. Two-D arrays form tabular, 2d arrangement. You can access elements with an index paira [i][j].
LENGTH use the length field to find the number in an array ArrayList
1. manages a sequence of objects 2. --ArrayList caqn grow and shrink as needed 3. the arraylist class supplies methods for many common tasdks, such as inserting and moving elements 4. to get objects out of the arraylist use the get method, not the [] operator
return statment
return expression; or return; Example return balance; Purpose to specify the value that a method returns, and exit the method immediately. The return value of the method call expression.
- This page was last modified on 2 December 2008, at 15:02.
- This page has been accessed 1,087 times.
- Privacy policy
- About ggc
- Disclaimers
- Powered by MediaWiki!









