Starting from:

$25

CSE1341 - Lab 2 - Solved

Create a Java program named Triangle.java that prompts for the base and the height of a triangle.  It then calculates and displays the area of the triangle. 

$ java Triangle 

Please enter a value for the base of a triangle? 4 
Please enter a value for the height of the triangle? 5 The area of the triangle is 10.0 
 

Bring the compiled program (.class file) along with the .java source file to your lab session.  Demonstrate the working program to your lab instructor for pre-lab credit. 

 

 

 

LAB [90 points]  

 

NOTES: Use the given notes as a guide for the program logic.  These comments must                be included in the programs to explain the logic followed. 

               Each program should compile without errors and should run to produce outputs                 described for each exercise.  The following points will be discounted if the related                element is missing or incorrect: 

 

              Reasonable output formatting [20 points] 

  Proper names for classes and variables [15 points] 

  Comments [15 points] 

  Program doesn't compile [ 20 points] 

  Source code (java file) missing [ 15 points] 

  Executable (class file) missing [15 points] 

 

 

The following programs are all based on the spreadsheet assignments you completed for lab 1. Complete the code for each of them so they compile and run successfully. 

 

 

 

1) [25 points] The Fibonacci sequence is a pattern of integers starting with zero and one, where each subsequent value is equal to the sum of the prior two values:  

                                    Fn = Fn-1 + Fn-2,   where F0 = 0 and F1 = 1. 

 

Create a Java program which will prompt the user with “How many numbers do you want to have in the sequence? (It must be greater than 2.)”.  Then it will prompt the user with “Do you want to skip the odd numbers? 1=yes, 2=no”.   

The program will then run according to the inputs from the user.  That is, if the user enters ‘12’ and ‘2’, then the program will just print the first 12 numbers in the sequence.  If the user enters ‘12’ and ‘1’, then the program will try to print the first 12 numbers but will replace the odd numbers with blanks as shown below.   

Sample runs:   

  

 

 

 

 

 

 

2)    [20 points] World Population  

The total world population was estimated at 7.6440 billion people in August 2018.  Assuming an annual growth rate of 1.09% per year, create a Java program showing the world population in 2018 and for each of the next 25 years.  Verify your results using the spreadsheet you created for Lab #1.    

Hint: if calculated properly, the U.S. population in 2043 would be 10.0236 billion people.   

  

 

 

 

 

3)    [25 points] Create a Java program named UnitConversion.java that converts the following:  

1.      Liter to gallon 

2.      Gallon to liter 

3.      Kilometers to miles 

4.      Miles to kilometers 

 

         The conversion formulas: 

1 liter 

0.264172 gallon 
1 gallon 

3.78541 liters 
1 kilometer  

0.621 mile 
I mile 

1.609 kilometers 
 

         

    Use the following sample data in your program - for grading purpose. 

 

  

 

 

 

 

 

 

4)    [20 points] Compound Interest Calculator 

The formula for annual compound interest, including principal sum, is:  A = P (1 + r/n) (nt) Where:   

A = the future value of the investment/loan, including interest  

P = the principal investment amount (the initial deposit or loan amount)  r = the annual interest rate (decimal)  n = the number of times that interest is compounded per year  t = the number of years the money is invested or borrowed for 

Create a Java program named CompundInterest.java which prompts the use to enter P, r, n, t and find the future value of the investment/loan, including interest (A) and the total interest earned.  Use the data in Lab 1 to test your program. 

(Lab1: Assume you put $120,000 into a bank. How much will your investment be worth after 15 years at an annual interest rate of 4% compounded quarterly?  How much will be the total interest earned?)  

More products