Starting from:
$35

$29

CS Algorithms & Programming Lab 2 Solution

The objective of this lab is to write basic Java programs that take inputs from the user and generate respective outputs on the console/terminal. The outputs are expected to have a certain formatting to achieve user friendliness. As always, this process will include program design & debugging. Remember that analyzing your problems and designing them on a piece of paper before starting implementation/coding is always a best practice.

0. Setup Workspace

Start VSC and open the previously created folder named labs. Now, under the labs folder, create a new folder named lab2.

In this lab, you are to have three Java classes/files (under labs/lab2 folder) as described below.

1. Rectangle Properties

Create a new/empty file of your own under the lab2 folder named Lab02_Q1.java with a class with the same name that takes two double inputs from the user to calculate the area, the circumference, and the diagonal of a rectangle. The user inputs are shown with blue color below.

Sample Run


Enter the width of the rectangle: 12.3

Enter the height of the rectangle: 24.1

The area of the
rectangle is
:
296.430
The
circumference of the rectangle is
:
72.800
The
diagonal of
the rectangle is
:
27.057


2. Maintenance Cost of a Car

Create a new/empty file of your own under the lab2 folder named Lab02_Q2.java with a class with the same name. This time your program will take the odometer (an instrument used for measuring the distance traveled by a vehicle) reading of the car in kilometers (km) as an input from the user (shown in blue) and will calculate and display a table as exemplified below. Total maintenance cost of the car is directly proportional with the kilometers travelled and is equal to 0.1 TL (constant) for each kilometer. That is, if the car has been driven for 10000 km, the total cost of maintenance is 1000 TL. The percentages shown indicate the percent of each maintenance item. Declare the percentages shown as constants; changing the value of these constants only should update the results. You should NOT repeat the space character in the output.
Sample Run


Enter the odometer reading of the car in kilometers: 60000

*************************************************

*****    Maintenance Cost Distribution Table    *****

*************************************************

*Oil Change
%30
1800.00
*
*Battery
%15
900.00
*
*Brakes
%25
1500.00
*
*Tire
%13
780.00
*
*Other
%17
1020.00
*
*
TOTAL
6000.00
*

*************************************************




3. Strings Incorporated

Create a new/empty file of your own under the lab2 folder named Lab02_Q3.java with a class with the same name. This time your program will take a title as an input from the user and display each one of the following in a separate line. A sample run is given below where the input is highlighted with yellow color. Note that, the entered title has 3 spaces at the beginning and 4 spaces at the end. You can use the Lang package’s String class and its methods. For details about the String class and possible methods to use, see: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html.

    a) Print number of characters in the string

    b) Convert all characters to uppercase and print

    c) Remove the leading and trailing whitespaces from the string and print

    d) Print the character at index 5

    e) Print the part of the title from 3rd character to 8th character (not included)

    f) Print the index of first occurence of character 'a'

    g) Print the index of last occurence of character 'a'


Sample Run


Enter the title:    War and Peace aaa

--------------------------------------

Number of characters:20

All uppercase:    WAR AND PEACE

Remove leading and trailing blank characters:War and Peace Character at index position 5:r

Title from 3rd character to 8th character (not included): War First occurence of character 'a':4 Last occurence of character 'a':13

More products