Download HOMEWORK: Programming Errors

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
HOMEWORK ASSIGNMENT:
PROGRAMMING ERRORS
PART I:
1.
Each of the following lines of code contains errors. For each line of code, circle the errors
and re-write the correct line of code in the space provided.
System.out.printn(“I LOVE JAVA!”);
_____________________________________________________________________
2.
int age = 16
_____________________________________________________________________
3.
System.out.println(St. Edmund Campion);
_____________________________________________________________________
4.
Scanner input = new Scanner(System.out);
_____________________________________________________________________
5.
system.out.println(“Hello world!”);
_____________________________________________________________________
6.
double price = input.nextInt();
_____________________________________________________________________
7.
import.java.text.DecimalFormat;
_____________________________________________________________________
8.
string city;
_____________________________________________________________________
9.
System.out.println(“My name is:” name);
_____________________________________________________________________
10.
public void static main(String args)
_____________________________________________________________________
11.
double wage = $12.00;
_____________________________________________________________________
12.
int x = 12.2
_____________________________________________________________________
Programming Errors
Page 1 of 3
13.
char initial = “B”;
_____________________________________________________________________
14.
System.out.println(num1 * num2 = num3);
_____________________________________________________________________
15.
public class 1_HelloWorld
_____________________________________________________________________
PART II:
Copy the following code into a blank Java document and save the program in a project
called Java Errors in your UNIT 2 folder. When you compile the program, you will be
notified of a number of compile-time errors. Correct each error until the program
compiles and runs properly.
import.java.util.Scanner;
public class Errors {
public static main(String[] args); {
// Declare variables
String num1;
int num2;
int sum;
// Declare variable for Scanner object
Scanner input = new Scanner (System.in);
// Prompt user for the first number
System.out.print(Please enter the first number:);
num1 = input.nextInt();
// Prompt user for second number
num2 = input.nextInt();
// Calculate sum
sum = num1 + num2
// Output the sum
system.out.println (num1 + num2 + “=” + sum);
}
}
Programming Errors
Page 2 of 3
PART III:
Identify whether the following errors are compile-time or run-time errors.
ERROR
COMPILETIME
RUNTIME
1. Forgetting a semi-colon at the end of a statement.
2. Forgetting to enclose Strings with quotations.
3. Trying to cast a double to an int.
4. You use the next() method to store more than one word.
5. Declaring a variable but not specifying the data type (e.g. int, char,
etc.).
6. Using the wrong tax rate to calculate tax on a subtotal.
7. Naming a variable that is a Java keyword.
8. Storing a number with decimals in an int variable.
9. The user inputs a word instead of a number.
10. The program tries to divide by zero.
Programming Errors
Page 3 of 3