Starting from:
$30

$24

Lab 02 --- Using Java to solve simple problems! Solved

Lab Objectives: ​Using Java to solve simple problems!


Notes:

    • For all labs in CS 101, your solutions must conform to these ​CS101 style guidelines​(rules!)

    • Create a Lab02 workspace (i.e. the folder H:\private\cs101\lab02). This assignment has parts a, b, c, & d, each of which should be placed in a separate project within the same Lab02 workspace. Note: only one project is active at a time. To work (Build/Run) a different project, right click on the project's name and select "Set as active project".

    • Ideally, you should declare all constants first, followed by all the variables you will use, followed by the actual program. Whilst constants should be given values when declared, variables should not! Unfortunately, the textbook usually gives variables initial values when declaring them, and declares them in the middle of the program code, as needed. In general, we consider this bad practice and recommend you do not do it!

    • These problems may be so simple you (think you) can do them in your head, however, it is a good idea to get into the habit of designing your program (on paper) first, before implementing it; doing so will save you a lot of time in the future when the problems get much larger.


    a. Demonstrate your ability to do basic output in Java using the println & printf methods. Your task is to produce the output shown below, including the car shape made with asterisk (star) symbols. Create a new project called Lab02a in your Lab02 folder. Declare a variable for the speed of the car, initialise it to 62.68 km/h, and then create the desired output.

Output:

    b. Create a new project called Lab02b.

You should use both input and output operations to construct a table showing a student's CS101 and CS201 grades. Examine the sample interaction below to design and implement your program. ​Note that the user inputs are shown in ​purple​. Collect all the relevant data from the user before printing the table. ​Hint: ​To get the output exactly right, including properly aligning table columns, you will need to use escape sequences to output characters such as new line, tab, slash, quotation mark, etc. Use named constants where appropriate.



Sample run:


    c. Practice doing mathematical computations in Java. Create a new project called Lab02c.

Your task is to find the total amount of paint required to paint a triangular prism. Input the three sides of the triangular end of the prism (a, b, and c), the height (h) of the triangle and the length (l) of the prism from the user (all in centimeters). The total surface area of the prism can be calculated using the equation given below. Assume that, we want to paint the entire prism and that 0.01 liters of paint are needed for one centimeter square, find the amount of paint required for this prism. The total amount of paint required should be of int type. Use named constants where appropriate. See a sample run below; the user inputs are shown in ​purple​.


























Sample run:

    d. Practice doing String operations in Java. Create a new project called Lab02d.

Write a program that reads a number between 1,000 and 999,999 from the user, where the user enters a comma after the thousands in the input. Then print the number without a comma. See a sample run below; the user input is in ​purple​.

Hint:​Read the input as a string. Think about how you can extract subsrings.

Sample run:

More products