Assignment 2 - Variables and Datatypes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Lab Assignment 02

Variables and Datatypes

CSE110: Programming Language I

Difficulty No of Tasks Points to


Beginner Intermediate Expert Total Score

Classwork 2 2 1 5 -
Evaluation - 1 1 2 20
Homework 3 3 3 9 90

The students must complete the classwork tasks in the lab class to obtain the lab performance
marks. The lab instructors may show/explain a few of the classwork tasks to the students if
necessary. Any plagiarism in classwork or homework will lead to the student getting zero in the
entire assignment.

Classwork
Beginner
1. Find out which of the following are legal identifiers in Java, and which are not. Also, explain
why the invalid ones are invalid. You can try to define each of these (items a to k below) as a
variable in the Dr java interaction pane and find out.

The first one is done for you as an Example. If you want to define hungry, you have to try
int hungry;

a) hungry b) 2AB c) 312.2 d) MOBILE e) “Ans” f) $30


g) Yes/No h) student-id i) A+3 j) ‘X’ k) return

2. Write the Java code for the following:


2.1 Declare an integer variable. Initialize it with some value of your choice and print it to
check the value has been stored properly.
2.2 Declare and initialize another integer variable. Add this to the first one and print out the
result. Verify that the addition has been done correctly.
2.3 Now print the product and division of the two integer numbers.
2.4 Repeat exercises 2.1, 2.2, and 2.3 for variables of data type double. Verify your answers.
2.5 Repeat exercises 2.1, 2.2, and 2.3 for one double data type and one integer datatype.
Verify your answers.
2.6 Repeat exercises 2.1 and 2.2 for variables of data type String. How does the addition
operator work for Strings? What if the first variable is an integer and the second is a String and
vice versa?

Intermediate
3. Write a Java program declaring two integer variables and initializing them. Your task is to
swap the values of these two variables. You must complete it using two different approaches.
● By Creating a third variable.

● Without creating any other variables.

4. Write a Java program to convert minutes into years and days. For simplicity, assume each
year consists of 365 days.

Test Data:
Given the number of minutes: 3456789
Expected Output:
3456789 minutes is approximately 6 years and 210 days
Expert

5. Design a Java program to calculate Sin and Cos values


from a right-angle triangle. Assume the values of a and b are
4.5 and 9.5 respectively. Finally, print the Sin and Cos
values of angle A and angle B (SinA, CosA, SinB, CosB).
The formulas to calculate these values are given below.

Hint: You need the values for all 3 sides to calculate both
sin and cos. You are given only a and b. How would you get
the value of c? You’ll need the help of Math.sqrt().
Evaluation

Intermediate

1. Suppose, you have three integer variables: a, b, c. Your first task is to assign the values 2, 5, 8
in these three variables. Next, you need to calculate and display the value of variable d using the
following formula:

Write a Java program based on this mentioned scenario that prints the value of d after
calculation.
[Answer: 27]

Expert

2. Write a Java program that displays the 2 rightmost digits of your student ID in reverse order.
For example, if your student id is 23221454, you need to print 4, and then 5.
[Hint: Use the logic you used in one of the tasks in lab 1]

Output:
4
5
Homework

Beginner
1. Write a Java code where given an integer we need to print the last 2 digits of that number.

2. Write a Java program that given a number in inches (you have to declare and initialize it
yourself) converts it to meters. Note: One inch is 0.0254 meters.

Test Data:
Given a value for inch: 1000
Expected Output:
1000 inch is 25.4 meters

3. Write Java code that calculates and prints the circumference and area of a circle with a radius
of 4 units.

Intermediate
4. Write a Java Code to display the multiplication table for a given positive integer 'n'. The table
should include the products of 'n' with each of the numbers from 1 to 10. For example, if n = 5,
your code should output:
5x1=5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50

[You are not allowed to use loops to solve this problem.]

5. Draw the flowchart of a program that finds the sum of the first 100 positive numbers.
[Do NOT use loops, use the mathematical formula for calculating sum of arithmetic series given
below].

Note:
S = n⁄2 (a + L), where n is the number of terms, a is the first term and L is the last term.

6. Write the Java code of a program that finds the sum of the first 100 positive numbers. You can
verify your answer by calculating this sum manually.
[Do NOT use loops, use the mathematical formula for calculating sum of arithmetic series given
below].

Note:
S = n⁄2 (a + L), where n is the number of terms, a is the first term and L is the last term.

Expert

7. You have been traveling on a bike for 5 hours, 56 minutes, and 23 seconds. Assuming the
distance covered is (Last 4 digits of your student ID). Write a Java code to display the velocity of
your bike in kilometers per hour and miles per hour. [Hint: 1 mile = 1609 meters]

Test Data:
Input distance in meters: 2500 // Assuming the last 4 digits are 2500

Expected Output:
Your velocity in km/h is 0.4208951
Your velocity in miles/h is 0.2615880

8. , 9.

Assume a Hexagon where each of the sides are of the same length.
From the visualization, we can see the values of a and b are given.
Your task is to (8) draw a flowchart and (9) write a Java code to
find the area and the circumference of the Hexagon.

You might also like