0% found this document useful (0 votes)
42 views

Lab2 - Java Variables

1. The document provides instructions for a lab on Java variables. It includes 5 exercises to introduce students to declaring and initializing variables, arithmetic expressions, increment/decrement operators, escape sequences, and comments. 2. The exercises guide students to create Java projects and files, declare and initialize different variable types, perform arithmetic operations, and analyze the effects of escape sequences and comments. 3. The lab aims to help students learn fundamental Java concepts like variables, expressions, and special characters through hands-on coding exercises.

Uploaded by

tvdxv7bjj8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lab2 - Java Variables

1. The document provides instructions for a lab on Java variables. It includes 5 exercises to introduce students to declaring and initializing variables, arithmetic expressions, increment/decrement operators, escape sequences, and comments. 2. The exercises guide students to create Java projects and files, declare and initialize different variable types, perform arithmetic operations, and analyze the effects of escape sequences and comments. 3. The lab aims to help students learn fundamental Java concepts like variables, expressions, and special characters through hands-on coding exercises.

Uploaded by

tvdxv7bjj8
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CCSE-Female Branch CSCE101 – Computer Programming Lab Manual 202301

Lab 2: Java Variables


Objectives:
By completing this lab, students should be able to:
• Learn how to declare and initialize variables and deal with arithmetic expressions.
• Learn how to use escape sequences and comments.

Instructions:
Task # 1: Creating a new Project in Eclipse.
1. Find the Eclipse icon in your PC. It should on Desktop. Then double click on the Eclipse
icon.
2. Select the Desktop as your workspace directory.
3. Go to File èNew è Java Project.
4. Write the Project Name “Lab2”. Then, Click Next è Finish.

Exercise1:
Creating a new java file called “Lab2Exercise1”.
1. Right-Click on the project folder you just create. You can find it in the left side of the editor.
Then, go to AddèNewè Class.
2. Write Lab2Exercise1 for the class name. Then, click Finish.
3. Copy the following code into the class editor.

SARAH ALFAYEZ-SARAH ALSWEDANI 1


CCSE-Female Branch CSCE101 – Computer Programming Lab Manual 202301

4. If there is no syntax error in your code, then run the file (go to Run on menu bar èRun)

5. The output should be like this:

Exercise2:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise2
2. Declare two variables int a, int b
3. Initialize the two variables above (a = 27, b = 10).
4. Calculate the addition, subtraction, multiplication, division, and modulo (remainder) of a and
b, and then display the result as following:

a + b is 37
a - b is 17
a * b is 270
a / b is 2.7
a % b is 7

SARAH ALFAYEZ-SARAH ALSWEDANI 2


CCSE-Female Branch CSCE101 – Computer Programming Lab Manual 202301

Exercise3:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise3
1. Declare three variables int x, double y, and float z.
2. Initialize the three variables above (x = 9, y = 2.3, and z = 5.2).
3. Display the result of each of the following arithmetic expressions using a System.out.println
( ) statement. In the Interpretation column, find out the precedence rules discussed in the
lecture.
Arithmetic Expression Result Interpretation
x+y*z
x/y*z
x/2+y/2
x/2
x%2
x%5*3+1
(y + 3) * 2
z / (1 + 1)

Exercise4:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise4
2. Declare an int variable i initialize it to 4, and then find the outputs of the following increment
and decrement statements following the same order:

Statement Output Interpretation


System.out.println (++ i);

System.out.println (i);

System.out.println (i++);

System.out.println (i);

System.out.println (--i);

System.out.println (i--);

System.out.println (i);

SARAH ALFAYEZ-SARAH ALSWEDANI 3


CCSE-Female Branch CSCE101 – Computer Programming Lab Manual 202301

Exercise5:
1. Add to the project, Lab2, a new java file and name it Lab2Exercise5
2. Copy the following code.

public class Lab2Exercise4 {


public static void main(String [] args){

// This exercise demonstrates escape sequences & comments (like these)!


System.out.print("Learn\tJava\n\tthe\nHard\tWay\n\n");
System.out.print("\tLearn Java the \"Hard\" Way!\n" );
System.out.print("Hello\n"); // This line prints Hello.
System.out.println("Surprised? /* abcde */ Or what did you expect?");
System.out.println( );
System.out.println("\\\\ / -=- \\ //" );
System.out.print("I hope you understand \"escape sequences\" now.\n")
}
}

3. Write the output here:

4. Analyze the result of the above code.

• What does \" do inside the quotations?


• What does \t do inside the quotations?
• What does \n do inside the quotations?
• What does \\ do inside the quotation?

SARAH ALFAYEZ-SARAH ALSWEDANI 4

You might also like