- Which of the following are valid Identifiers ? For the ones that are not, explain
a) coolHandLuke
b) cool_hand_luke
c) cool hand luke
d) 157RivesideAvenue
e) int
f) intValue
h) _myScore
- True or False, if false explain.
Given x = 7, y = 3, z = 2
x + y * z
is the same as
(x + y ) * z
- For each of the following, what is the value of z ?
Given a = 2, b = 3, d = 6
a) z = d/2 - d/3
b) z = d % c - a
c) z = c % b * a;
- Which of the following statements are incorrect? Explain !
Given:
int b;
double c;
final int x = 12;
1) b = Math.sqrt(16);
2) c = 1000;
3) x += 12;
- The following class definition has EXACTLY four errors, find them
public CLASS MATH {
public square ( base ) {
return base * base ;
}
- An instance method named greetings will print a greeting to the console, hence
does not return a value. Is the following an acceptable definition
for that method ?
public greetings() {
System.out.println("Welcome");
}
- Write a simple if statement that prints the word true to the console if
an integer variable x is not equal to 10.
- Correct the error for each of the following.
a) if( y > 10)
z = 3;
y = 9;
else
w = 2;
==============================================
b) if ( z = 7 )
y = 7;
else
y = 8;
==============================================
c) if ( x == 0 )
then z+= 12;
===============================================
d) if ( x < 0 ) status = "negative";
else if ( x > 0 ) status = "positive";
else ( x == 0 ) status = "zero";
=================================================
e) int x = 0
while ( x <= 4 ) {
y = x;
--x;
}
==================================================
f) int x = 4;
while ( x != 14 );;
x += 1;
==================================================
g) int x = 15;
while ( x > 12 )
System.out.println( x );
--x;
==================================================
- For each one, what is output to the console ?
a) int x = 17;
if( x >= 7 )
System.out.println("x is greater than 7 ");
else
System.out.println("x is " + x );
System.out.println("x is less that 7 ");
=========================================================
b) int x = 0;
while ( x <= 4 ) {
System.out.println("x is " + x );
x += 2;
}
===========================================================
c) for (int i = 0; i < 5; i++)
System.out.println("i is " + i );
System.out.println("Looping Again ?");
============================================================
d) for(int j = 0; j <= 10; j += 2)
System.out.println("j is " + j);
- Explain the difference between a while statement and a do-while
- Write a complete Java program that uses a pop up window to
get an integer from the user. If the integer is bigger than
1000 print out to the console "big" else
print "small"
Before you answer 11 - 14, review lab 5 question 7.
- Write a method called findMin that accepts two integer parameters
and returns the smaller of the two.
- Write a method called average that accepts four
integer parameters and returns their average as a floating point value.
- Write a method called computeTax that accepts one floating point parameter
that represents your income and returns the tax you owe based on this
simple schedule.
0 to 100000 5%
100000 to 200000 10%
over 200000 15%
less than 0 0%
- Write a method called isEven that accepts one integer and returns true if the
integer is even false otherwise.
- Use the class definition to answer the following quastions.
public class Rectangle {
private int length;
private int width;
public Rectangle(int l, int w){
length = l;
width = w;
}
public int computeArea(){
return length * width;
}
public int getLength(){
return length;
}
public int getWidth(){
return width;
}
}
- Will the following statement work ? , if not explain
Rectangle square = new Rectangle(4);
- Write the statements to find the area of a rectangle with length 529 and width 76
You must use the method
computeArea from the Rectangle class.
- True or false, if false you must explain. Which statements in the main method will compile
which will not.
public class Test{
public static void main(String [] args){
Rectangle room1 = new Rectangle(55,88); //1
System.out.println("length is " + room1.length); //2
System.out.println("the area of the room is " + computeArea() ); //3
room2 = new Rectangle(33,55); //4
}
}
- Using the Rectangle class what is the output ?
Rectangle room1,room2,room3;
room1 = new Rectangle(33,55);
room2 = new Rectangle(55,33);
room3 = new Rectangle(33,55);
if( room1.getLength() == room2.getLength())
System.out.println("Same length");
else
System.out.println("Different length");
- Design a class named Book with the following specification
Instance variables:
- title
- price
Constructor:
- one to create a book object with title and price;
Methods:
- Accessor methods, one for each instance variable
- A method to change the price.
- Create an object of type Book and assign it to a reference variable called
myBook. Give any title and price.
- Lab 4 questions 1 - 5
- Lab 5 questions 1 - 7.