Docsity

Java Fundamentals Lab Manual, Lab Reports of Programming Languages

Partial preview of the text

Download Java Fundamentals Lab Manual and more Lab Reports Programming Languages in PDF only on Docsity!

Spring 2018

CS-104/114: Introduction to Programming /

Programming Fundamentals

Lab- 3 Manual

Java Fundamentals

v1. 4/8/

GIFT School of Engineering

and Applied Sciences

Task #1: Using correct numeric datatypes

In this task, you are being asked to write a program that calculates the average of numbers.

  1. Create a program called Averages.java having the main() part.
  2. Create five integer variables called number1 to number5. Assign proper values to these five variables.
  3. Create a variable called sumIntegers and assign the sum of all five variables to sumIntegers.
  4. Create a variable called averageIntegers, and assign the average of all five variables to averageIntegers.
  5. Use the formula: average = sum of all numbers / count of numbers.
  6. Print all five numbers, their sum, and their average by giving appropriate messages.
  7. Repeat the same exercise for five double variables ( number6 to number10 ).
  8. Create variables to store their sum and calculate their average.
  9. Print all five numbers, their sum, and their average by giving appropriate messages.

Task # 3 : Writing and evaluating expressions

In this task, you are being asked to understand the evaluation order of various operators. Complete the following table by writing the value of each expression in the Value column without using a calculator and the computer.

Expression Value

Now, crate a program called Expressions.java, and write output statements and print both the expression and its value.

Task # 4 : Integer and floating-point division

In this task, you are being asked to understand the difference between integer and floating-point division in Java. Complete the following table by writing the value of each expression in the Value column without using a calculator and the computer.

Expression Value

Now, crate a program called Divisions.java, write output statements and print both the expression and its value.

Task # 6 : Correcting errors in a program

In this task, you are being asked to identify and correct the errors in a program. There are a number of syntax errors in the following program. Locate and correct as many as you can. / What's wrong with this program? / public MyProgram { public static void Main(String args); } int a; b, c \ Three integers 3 = a b = 4 a + b = c System.out.println('The value of c is' + C); {

  1. Create a program called MyProgram.java.
  2. Type the program as given above.
  3. Correct all errors.
  4. Run and see the output.

Task # 7 : Using E (or e) notation

In this task, you are being asked to correctly use the E / e notation to represent floating-point numbers. First, write each of the below numbers as floating-point numbers without using the powers of 10 by hand (no calculators please). Second, how would each of the following numbers be represented in E / e notation? a. 3.287 Ɨ 106 b. - 9.7865 Ɨ 1012

c. 7.65491 Ɨ 10 -^3

d. - 153. 5432 Ɨ 10 -^5

  1. Create a program called ENotation.java.
  2. Create three appropriate variables to store the above three given numbers as E notation.
  3. Print each statement and its output.

Task # 9 : Converting word problems into programs

In this task, you are being asked to write a given word problem as a program. One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of acres in a tract of land with 389,767 square feet. Hint: Divide the size of the tract of land by the size of an acre to get the number of acres.

  1. Create a program called LandCalculation.java.
  2. Create appropriate variables with values as given in the statement above.
  3. Run and print the required value.

Task # 10 : Converting word problems into programs

In this task, you are being asked to write a given word problem as a program. Create a variable to store the amount of a purchase as a real value. The program should then compute the state and county sales tax. Assume the state sales tax is 4 percent and the county sales tax is 2 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the total sales tax).

  1. Create a program called SalesTax.java.
  2. Create appropriate variables with values as given in the statement above.
  3. Run and print the required values.

Task # 12 : Converting word problems into programs

In this task, you are being asked to write a given word problem as a program. A cookie recipe calls for the following ingredients:

  • 1.5 cups of sugar
  • 1 cup of butter
  • 2.75 cups of flour The recipe produces 48 cookies with these amounts of the ingredients. Create a variable that contains how many cookies are to be made (as an integer), and then displays the number of cups of each ingredient needed for the specified number of cookies.
  1. Create a program called Ingredients.java.
  2. Create appropriate variables with values as given in the statement above.
  3. Run and print the required values.