W02Assignment 11-2014
description
Transcript of W02Assignment 11-2014
Week 2 Homework
Problem 1 (3 points)
Answer the following:
a) What is an identifier (variable name)?
Identifiers are the names of variables, methods, classes. In a system program, the String and println are identifiers. It tells the program what to look for and do exactly what is needed. The variable has a name in the program.
b) What are the rules for naming an identifier (variable name)?
Variables can only start with a letter, and underscore (_) or a dollar sign $ and must have no spaces. When calling a variable, it is important everything matches as they are case senstitive.
1
Problem 2 (3 points)
Identify the type of data, integer, double, or string, and a valid identifier (variable name) for the following types of data:
a) permanent city
String
b) average number of students in a class
Double
c) total number of students in a class
Integer
d) grade point average
Double
e) client name
String
f) insurance discount based on age
Integer for ageDouble for discount (number times % = decimal)
This can also be a boolean. Does the customer qualify for the age? If yes, then what discount based off age, if NO then they do not qualify.
Problem 3 (3 points)
Identify the syntax errors in the following identifier (variable) definitions
a) int 1num = 0;
2
The variable can not start with a number.int num1 = 0; would be correct
b) int avg = 2.8;
Cannot have 2.8 with int.doube avg = 2.8; would be correct
c) String first-last-name = “John Doe”;
The variable cannot use the character - to identify it.String firstlastName = "John Doe"; would be correct.
d) String return = “return address”;
The variable 'return' is used to return a value. This is causing a syntax error because the program is looking for something to return. By changing the word return to returnAddress will fix the error.
e) String student name = “John Doe”;
The variable, 'student name' has a space. This can be fixed by changing it to studentName or student_name.
f) double rbi = “3.8”;
Remove the " " around 3.8 and that will fix this syntax error.
3
Problem 4 (3 points)
Copy the following program into BlueJ, compile, and execute.
import java.io.*;import java.lang.*;import java.util.*;
public class Art{ public static void main(String[] args) { int n = 15; for (int i = 1; i <= n; i++) { for (int j = 0; j < (n - i); j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); for (int k = 1; k < i; k++) System.out.print("*"); System.out.println(); }
for (int i = n - 1; i >= 1; i--) { for (int j = 0; j < (n - i); j++) System.out.print(" "); for (int j = 1; j <= i; j++) System.out.print("*"); for (int k = 1; k < i; k++) System.out.print("*"); System.out.println(); }
}}
What does this program do?
It draws out two diamonds using *. The first diamond has the top section cut off and the second diamond under it is an entire diamond.
4
Problem 5 (3 points)
Copy the following program into BlueJ. Compile and execute. Use this code to answer the following questions:
public class Cube { public static void main() { double height = 3.0; \\ inches double cubeVolume = height * height * height; double surfaceArea = 8 * height System.out.print("Volume = " + surfaceArea); System.out.print("Surface area = + surfaceArea); }
a) The code above contains several syntax errors. Create a new class in BlueJ named Cube. Delete any code generated by BlueJ in the Cube class. Copy the code above and past into the class. Compile. List and correct each syntax error.
On line 5, the character \ is illegalOn line 7, there is a missing ; at the endOn line 9 there is a missing " after the =There is a missing } at the end of the code. This closes the class Cube
b) Copy the corrected code below.
public class Cube { public static void main() { double height = 3.0; double cubeVolume = height * height * height; double surfaceArea = 8 * height; System.out.print("Volume = " + surfaceArea); System.out.print("Surface area = " + surfaceArea); }
}
5
6
c) The correctly compiled code contains 2 logic errors. Identify and correct the logic errors. Copy your corrected code below.
The variable surfaceArea is where Volume is. This is wrong. It should be cubeVolume.
Also, the surfaceArea calculation of the cube is wrong as well. It should be all sides of the cube (6) multiplied by the height x height of the cube.
Instead of 8 * height, it should be 6 * (height * height)
public class Cube { public static void main() { double height = 3.0; double cubeVolume = height * height * height; double surfaceArea = 6 * (height * height); System.out.print("Volume = " + cubeVolume); System.out.print("Surface area = " + surfaceArea); }
}
7