Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming Exercise 1: Comments and Output Statements - Prof. Carol L. Spradling, Assignments of Computer Science

Java programming exercise solutions for writing comments and output statements. It covers one-line and multi-line comments, displaying text on the screen, and data types with examples. Java code snippets are included for variable declaration, assignment, and output.

Typology: Assignments

2009/2010

Uploaded on 02/23/2010

apalmer14
apalmer14 🇺🇸

4 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
44-141 Computer Programming I
Exercise 01
1. How does one write a one line comment in Java?
// comment to end of line
2. How does one write a comment in Java that extends to more than one line?
/*comment*/
3. Write the single line of Java code that will display (output) the word ‘hello’ on the screen
(without the quotes).
System.out.print(“Hello”);
4. Write two lines of Java code that will display the following text on two lines.
This is the first line
This is the second line
System.out.println(“This is the first line”);
System.out.print(“This is the second line”);
5. Provide an example of the type of data that might be stored with each data type.
a) int - -1, 0, -5, 3
a) double – 3.6, -2.8
b) char – ‘a’, ‘#’,’l’
c) boolean – true or false
d) String – “Hi!”
6. Write the Java code to declare a variable named examScore that will store whole
numbers (no decimal values).
int examScore;
7. Write the Java code to assign the value 10 (an integer) to the declared variable named
examScore.
examScore=10;
8. Write the Java code to declare and assign the value 10.5 (a double dateype) to a
variable named price.
double price=10.5;
9. Write the Java code to display the contents of the variable examScore to the
monitor.
System.out.print(examScore);
10. Write the Java code to display the contents of the variable price to the monitor.
System.out.print(price);
Challenge Problems (You are not required to complete the challenge problems):
1. Write one line of Java code that will display the following text on two lines as
shown below.
This is the first line
This is the second line
System.out.print(“This is the first line\nThis is the second line”);
2. Write one line of Java code that will display the following text on one line with a
tab character separating the text as shown below.
This line contains a tab character
pf2

Partial preview of the text

Download Java Programming Exercise 1: Comments and Output Statements - Prof. Carol L. Spradling and more Assignments Computer Science in PDF only on Docsity!

44-141 Computer Programming I Exercise 01

  1. How does one write a one line comment in Java? // comment to end of line
  2. How does one write a comment in Java that extends to more than one line? /comment/
  3. Write the single line of Java code that will display (output) the word ‘hello’ on the screen (without the quotes). System.out.print(“Hello”);
  4. Write two lines of Java code that will display the following text on two lines. This is the first line This is the second line System.out.println(“This is the first line”); System.out.print(“This is the second line”);
    1. Provide an example of the type of data that might be stored with each data type. a) int - -1, 0, -5, 3 a) double – 3.6, -2. b) char – ‘a’, ‘#’,’l’ c) boolean – true or false d) String – “Hi!”
    2. Write the Java code to declare a variable named examScore that will store whole numbers (no decimal values). int examScore;
    3. Write the Java code to assign the value 10 (an integer) to the declared variable named examScore. examScore=10;
    4. Write the Java code to declare and assign the value 10.5 (a double dateype) to a variable named price. double price=10.5;
    5. Write the Java code to display the contents of the variable examScore to the monitor. System.out.print(examScore);
    6. Write the Java code to display the contents of the variable price to the monitor. System.out.print(price); Challenge Problems (You are not required to complete the challenge problems):
    7. Write one line of Java code that will display the following text on two lines as shown below. This is the first line This is the second line System.out.print(“This is the first line\nThis is the second line”);
    8. Write one line of Java code that will display the following text on one line with a tab character separating the text as shown below. This line contains a tab character

System.out.print(“This line contains \ta tab character”);