Description
For all labs, your solutions must conform to the CS101 style guidelines!
All data and results should be stored in variables (or constants where appropriate) with meaningful names.
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. Ellipse 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 and the circumference of an ellipse. The user inputs are shown with blue color below.
Sample Run
Enter length of the semi major axis of the ellipse: 24.1
Enter length of the semi minor axis: 12.3
The area of the ellipse is : 931.262
The circumference of the ellipse is ~ : 120.213
The area of an ellipse is , where and denote the semi major and semi minor axes of the for the purposes of this exercise: 𝐶 ≃ 2π · 𝑎2+2𝑏2 . Take π as 3.14159265359.
ellipse. The circumference𝐴 of= theπ · ellipse𝑎 · 𝑏 is a lot𝑎more𝑏difficult to calculate but an approximation will suffice
2. Maintenance Cost of a Car
– Site Work – $15,903
– Foundations – $25,671
– Framing – $41,123
– Exterior – $33,066
– Utility – $32,746
– Interior Finishes – $67,939
– Outdoor – $16,591
– Unexpected Costs – the rest
The percentages shown indicate the percent of the cost of each construction item. First define appropriate constants to represent the cost of each item in an average house. Changing the value of these constants in your code should update the results. Then, calculate the percentages of each item based on these constants. These percentages should also be constants.
Sample Run
Enter the cost of building a house in USD: 300000
*************************************************
***** House Cost Distribution Table *****
*************************************************
* Site Work %6 20066.03 *
* Foundations %10 32391.07 *
* Framing %17 51888.04 *
* Exterior %13 41721.90 *
* Utility %13 41318.14 *
* Interior Finishes %28 85723.84 *
* Outdoor %6 20934.14 *
* Unexpected Costs %1 5956.85 *
* TOTAL 300000.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 lowercase and print
c) Remove all leading and trailing white spaces from the string and print
d) Print the character at index 4
e) Print the part of the title from 3rd character to 5th character (both included)
f) Print the index of first occurrence of character ‘e’
g) Print the index of last occurrence of character ‘e’
Sample Run
Pride & Prejudice o
Enter the title:
————————————–
Number of characters:24
All lowercase:” pride & prejudice ”
Remove leading and trailing blank characters:”Pride & Prejudice”
Character at index position 4:’r’
Title from 3rd character to 5th character (both included):” Pr”
First occurrence of character ‘P’:3
Last occurrence of character ‘P’:11
Reviews
There are no reviews yet.