0% found this document useful (0 votes)
9 views4 pages

Week 4 Lab

The document is a lab guide for Programming 1 focusing on Java variables, specifically numerical data types. It outlines the different numeric types in Java, how to declare and initialize variables, and provides several exercises for practical application, including calculating area, tracing code for errors, and converting Celsius to Fahrenheit. The document serves as an instructional resource for students in the College of Computer Science and Engineering in Yanbu.

Uploaded by

fnydxpnhpb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Week 4 Lab

The document is a lab guide for Programming 1 focusing on Java variables, specifically numerical data types. It outlines the different numeric types in Java, how to declare and initialize variables, and provides several exercises for practical application, including calculating area, tracing code for errors, and converting Celsius to Fahrenheit. The document serves as an instructional resource for students in the College of Computer Science and Engineering in Yanbu.

Uploaded by

fnydxpnhpb
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programming 1

Lab 3: Java Variables (Numerical Data Types)


• Variables are containers for storing data values.

• Variables are used to represent values that may be changed in the program.

• Variables are for representing data of a certain type.

In Java, there are different types of variables, for example, String, char, Boolean,
and other numerical types. In this lab our focus will be in the numerical types. In
Java, there are six numeric types four integer and two floating point:

1. byte (1 byte), -128 to 127

2. short (2 bytes), -32,768 to 32,767

3. int (4 bytes), -2,147,483,648 to 2,147,483,647

4. long (8 bytes),, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80

5. float (4 bytes), 7 decimal digits

6. double (8 bytes), 16 decimal digits

Declaring Variables
To declare a variable of one of the numeric types you would use the form:
datatype variableName;
For example, int x ;

Variables Assignment or Initialization


To fil up the declared variable with an appropriate value you would use the form:
variableName = value;
For example, x = 2 ;
There are other ways to initialize a variable as discussed in class.

College of Computer Science and Engineering in Yanbu 2021


Programming 1

Exercise1
a) Using eclipse, create a new java project called myProject2.

b) In myProject create a new class called “rectangleArea”

c) In the class, add the driver method “ public static void main (String [ ] args) ”

d) In the driver method, declare three variables (width, length, area), use double as
the data type.

e) Then, initialize your variable as follows

• width = 3

• length = 4

• area = width x length

f) Print the result of the area

Exercise 2
What is the output of the following code ?

College of Computer Science and Engineering in Yanbu 2021


Programming 1

Exercise 3
What is the output of the following code ?

Exercise 4
Trace the following code, to find and solve the syntax error. You need to write the line
number, the mistake and the solution.

Exercise 5
Trace the following code, to find and solve the syntax error. You need to write the line
number, the mistake and the solution.

College of Computer Science and Engineering in Yanbu 2021


Programming 1

Exercise 6

Write a Program to find the average of three numbers.

Exercise 7

Write a Program to convert the Celsius to Fahrenheit

Hint: Fahrenheit = (9/5 × C) + 32

Assign Celsius = 90 and find the output.

College of Computer Science and Engineering in Yanbu 2021

You might also like