Starting from:

$29

Java Programming Assignment 01

CSC 3020
Java Programming
Assignment 01
50 points


Assignment Objectives:
■■ To understand the meaning of Java language specification, API, JDK™, JRE™.
■■ To create, compile, and run Java programs.
■■ To explain the runtime errors.
■■ To obtain input from the console using the Scanner class.
■■ To explore Java numeric primitive data types: byte, short, int, long, float, and double.
■■ To distinguish between postincrement and preincrement and between postdecrement and predecrement.
■■ To cast the value of one type to another type.
■■ To declare boolean variables and write Boolean expressions using relational operators.
■■ To generate random numbers using the Math.random() method.
■■ To program using selection statements for a variety of examples.
■■ To combine conditions using logical operators (!, &&, ||, and ^).
■■ To implement selection control using switch statements.
■■ To examine the rules governing operator precedence and associativity.
■■ To compare and test characters using the static methods in the Character class.
■■ To represent strings using the String class.
■■ To format output using the System.out.printf method.

All assignments must be submitted by the Canvas. No email or hard copy is accepted. You must follow the following format:
a.    For non-programming questions, use a word file to type your answers. Don’t use the text box on the Canvas to answer the questions or to write comments, we will not read it. 
b.    State your answer clearly.
c.    For programming questions, include only the source file for each problem.
d.    Submit your file to the Canvas. You must submit your assignment on time; otherwise, you will receive zero. In addition, you cannot submit your file more than one time. 
e.    There will be several folders on the Canvas. You need to upload your file(s) using the correct folder on the Canvas.
f.    Name each file:  “Assignment Number(Question number(s))”.
g.    To upload your file(s):
•    In Course Navigation, click the Assignments link.
•    Click the title of the assignment.
•    Click the Submit Assignment button.
•    Add File. ...
•    Add Another File. ...
•    Submit Assignment. ...
•    View Submission.

It is your responsibility to make sure that each file is uploaded correctly. If you uploaded a wrong file, you receive zero; files will not be accepted after due date even if you have a prove that the file is created before the due date. 



Answer questions 1 to 7 on a word file; write a program for each of Q.8 - Q.10.

1.    (13 points) Give a brief answer for each of the following questions:

a)    What is the difference between an interpreted language and a compiled language?
 
b)    What does JDK stand for? What does JRE stand for?

c)    What is the Java source filename extension, and what is the Java bytecode filename extension?
 
d)    What is the command to compile a Java program?
 
e)    What is the command to run a Java program?
 
f)     Explain the two compilation phases of Java programs.

g)    Show the output of the following code:
        double amount = 5;
        System.out.println(amount / 2);
        System.out.println(5 / 2);
         
h)    What data types are required for a switch variable? If the keyword break is not used after a case is processed, what is the next statement to be executed? 
 
i)    What is y after the following switch statement is executed? Rewrite the code using an if-else statement.
        x = 3; y = 3;
        switch (x + 3) {  
              case 6:  y = 1;
              default: y += 1;
        }

j)    Why does the Math class not need to be imported?
 
k)    Which of the following are correct literals for characters?
'1', '\u345dE', '\u3fFa', '\b', '\t'
 
l)    Write the code that generates a random lowercase letter.

m)    What is wrong in the following code?
import java.util.Scanner;

public class Test {
  public static void main(String[] args) {       
    Scanner input = new Scanner(System.in);
    System.out.print("Enter an integer: ");
    int value = input.nextInt();
    System.out.println("The value is " + value);

    System.out.print("Enter a line: ");
    String line = input.nextLine();
    System.out.println("The line is " + line);
  }
}            
 
2.    (2 points) State whether each of the following is true or false. If false, explain why.
 
3.    (2 points) Write an expression that obtains a random integer between 34 and 55 inclusive. Write an expression that obtains a random integer between 0 and 999. 


4.    (2 points) Can the following conversions involving casting be allowed? If so, find the converted result.
char c = 'A';
int i = (int)c;

float f = 1000.34f;
int i = (int)f;

double d = 1000.34;
int i = (int)d;

int i = 97;
char c = (char)i;

5.    (5 points) Suppose that s1, s2, and s3 are three strings, given as follows:
String s1 = "Welcome to Java";
String s2 =  "Programming is fun";
String s3 =  "Welcome to Java";
What are the results of the following expressions?
a.    s1 == s2
b.    s1 == s3
c.    s1.equals(s2) 
d.    s1.equals(s3) 
e.    s1.compareTo(s2) 
f.    s2.compareTo(s3) 
g.    s2.compareTo(s2) 
h.    s1.charAt(0)
i.    s1.length()
j.    s1.toLowerCase()

6.    (4 points) Suppose that s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
String s1 = "Welcome to Java";
String s2 = "Welcome to Java";
String s3 = s1 + s2;
String s3 = s1 - s2;
s1 == s2;
s1 >= s2;
s1.compareTo(s2);
char c = s1(0);
char c = s1.charAt(s1.length());


7.    (4 points) Show the output of the following statements.
(a) System.out.printf("amount is %f\n", 32.32);
(b) System.out.printf("amount is %5.2f%\n", 32.327);
(c) System.out.printf("%6b\n", (1 > 2));
(d) System.out.printf("%6s\n", "Java");
(e) System.out.printf("%-6b%s\n", (1 > 2), "Java");
(f) System.out.printf("%6b%-8s\n", (1 > 2), "Java");
(g) System.out.printf("%,5d %,6.1f\n", 312342, 315562.932);
(h) System.out.printf("%05d %06.1f\n", 32, 32.32);






Programming Questions
8.    (6 points) Write a program that inputs from the user the radius of a circle as an integer and prints the circle’s diameter, circumference and area using the floating-point value 3.14159 for π. [Note: You may use the predefined constant Math.PI for the value of π. This constant is more precise than the value 3.14159. Class Math is defined in package java.lang. Classes in that package are imported automatically, so you do not need to import class Math to use it.] Use the following formulas (r is the radius):
•    Diameter = 2 * r
•    Circumference = 2 * pi * r
•    Area = pi * r * r
Do not store the results of each calculation in a variable. Rather, specify each calculation as the value that will be output in a System.out.printf statement. The values produced by the circumference and area calculations are floating-point numbers. Such values can be output with the format specifier %f in a System.out.printf statement.

 


9.    (6 points) Write a program that prompts the user to enter a Social Security number in the format DDD-DD-DDDD, where D is a digit. Your program should check whether the input is valid. Here are sample runs:

 

10.    (6 points) Assume that a vehicle plate number consists of three uppercase letters followed by four digits. Write a program to generate a plate number.
 

More products