0% found this document useful (0 votes)
147 views6 pages

CS110T Lab03 PDF

This document describes a lab assignment on Java basics. The lab objectives are to practice defining variables of different data types, reading user input, compiling and executing Java applications, and writing and evaluating mathematical expressions. The lab exercises include determining the output of sample Java code, writing Java statements to declare and initialize variables, debugging code with syntax errors, and writing a program to calculate costs and profits from milk production based on user input.

Uploaded by

m
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)
147 views6 pages

CS110T Lab03 PDF

This document describes a lab assignment on Java basics. The lab objectives are to practice defining variables of different data types, reading user input, compiling and executing Java applications, and writing and evaluating mathematical expressions. The lab exercises include determining the output of sample Java code, writing Java statements to declare and initialize variables, debugging code with syntax errors, and writing a program to calculate costs and profits from milk production based on user input.

Uploaded by

m
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
You are on page 1/ 6

CS110T: Programming Language1

Lab 03: Java basics II

Lab Objectives:
In this lab, the student will practice:
ü Defining variables of different data types
ü Reading data values from the user.
ü Compiling and executing Java applications.
ü Writing and evaluating mathematical expressions
Lab Exercise 1: Program Output
Problem Description: What is the output of the following code?

// Lab Exercise 1: Math.java


// Computing and displaying the result of different expressions

public class Math


{
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.println("4.5+1="+(4.5+1));
System.out.println("4.0+9="+4.0+9);
System.out.print("((2-2) × 1×3) - 3/1.0= ");
System.out.println(((2-2)*1*3)-3/1.0);
System.out.println("(1+2)*3*4/5 ="+(1+2)*3*4/5);
}
}

Output:

2
Lab Exercise 2: Java statements
Problem Description: Write Java statements that accomplish the following:

a. Declare two variables x as an integer number and y as a character.

b. Initialize x to 10 and y to B.

c. Update the value of x by adding 5 to it.

d. Declare a variable C as character and copy the value of y into it.

3
Lab Exercise 3: Debugging
Problem Description: The following program has syntax errors. Correct them, then type and
compile the program to check if all errors have been found. (Hint: 7 errors must be found)

public class ProgAWithErrors


{
public void main (String [] arg)
{
Int one, two;
double first, second;

one = 18;
two ="apple";

first = 25.5;
second = first * three;

System.out.printf("%d \t %f \t %s\n",first,second,(second/3));

}
}

The solution :

4
Lab Exercise 4: Coding
Problem Description: A milk carton can hold 3.78 liters of milk. Each morning, dairy farm
ships cartons of milk to local grocery store. The cost of producing one liter of milk is $ 0.38
and the profit of each carton of milk is $ 0.27.

Write a java program that does the following:


1. Get from the user the amount of milk produced in the morning in liters.
2. Output the number of milk cartons needed to hold the milk.
3. Output the cost of the producing milk.
4. Output the profit for producing milk.

A simple Output
How many liters of milk you need this morning?
6
you need 2.0 cartons for the 6.0 liter of milk
The total cost is 2.87
The total profit is 0.54

code:

5
Assignment Problems:
Problem Description 1: Consider the following program segment:
public class Prog1
{
public static void main (String [] arg)
{
// variable declaration

// executable statements

}
}

a. Write a java statement that imports class Scanner.


b. Declare input to be Scanner object for inputting data.
c. Get two integer numbers from the user then store them in num1 and num2. (declare variables if
needed)
d. Update the value of num1 by Multiplying it by 2
e. Add the value of num2 to num1 and store them in NewNum
f. Display the value of NewNum.

Problem Description2: Write a program that asks the user to type the price without tax of one kilogram of
tomatoes, the number of kilograms you want to buy. The program must calculate the total price including
taxes.
Note: taxes are 5% of product price.

Simple Output:
Please enter the price without tax of one kilogram of tomatoes: 10
Please enter the number of kilograms: 5
Total price: 52.5 S.R.

You might also like