CS101 Lab 4 Solved

34.99 $

Category: Tags: , ,
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (1 vote)

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 learn how to use a while loop to implement automated repetition. Remember that analyzing your problems and designing them on a piece of paper before starting implementation/coding is always a best practice.
In this particular lab, only use the while loop, do not use the for or do-while loops.
0. Setup Workspace
Start VSC and open the previously created workspace named labs_ws. Now, under the labs folder, create a new folder named lab4.
In this lab, you are to have four Java classes/files (under labs/lab4 folder) as described below. A fifth and sixth Java file containing the revisions should go under this folder as well. We expect you to submit a total of 6 files including the revisions. Do not upload other/previous lab solutions in your submission. The user inputs in the sample runs are shown in blue.
1. Number Wizarding
Write a program that takes a positive integer from the user, inverts it and prints the result. Check for
non-integer inputs and non-positive integers as well.𝑛
Sample run:
Enter input n: 12345
54321
2. Decimal to Binary Conversion
it out. No need for validating input (assume the user enters an integer).𝑛 Write a program that takes an integer from the user, converts it to binary, stores it as an integer and prints
Sample runs:
Please input n: 10 1010
Please input n: 0103
1100111
3. Divide/Multiply by Bit Shifting
There are a few ways of achieving division and multiplication operations in math and “bit shifting” is one of them for any number system. We are going to utilize a property of number systems to divide and multiply by
2 in binary, similar to the decimal representation.
Let’s take a binary number 10, which is 0b1010 in the binary number system. Notice that the prefix 0b indicates that the number will be interpreted as a binary number. Bit shifting means shifting all the digits either to left or right. When we shift all the digits to the left, we add “0” to the end of the binary number in
Java. When we shift right, we discard the least significant bit (lsb), which is the first bit that has a 20 coefficient. Let’s see this with an example.
The number is decimal 10 and binary 0b1010.
0𝑏1010 = 1If we right shift the number, it is going to be 0b101.* 23 + 0 * 22 + 1 * 21 + 0 * 20 = 8 + 0 + 2 + 0 = 10
0𝑏101 = 1 * 22 + 0 * 21 + 2 * 20 = 4 + 0 + 1 = 5
Check these two questions. What happens if you perform a division by an odd number? What happens if you right shift a decimal number?
Same logic is applicable for the left shift. However, adding 1 or 0 to the right side of a number depends on the compiler you use.
multiples of 2 with bit-shifting operations. Your program should𝑛 ask for user input until the user wants to Your task is to write a program that takes a positive integer from the user and divides or multiplies by
terminate the mathematical operation task. No need to validate input.
Prompts of your program might be such questions: “Do you want to continue?” or “Do you want to calculate again?”. But make sure your prompt is clear and carry the exact intention of yours. For example “Do you want to continue?” is not clear. It is open ended.
Computer programs can be deterministic yet people’s behaviors will vary. Possibilities, edge cases will come true in real life applications.
Tip1: There are many coding ways of starting a loop and terminating a loop based on user input; some are more elegant than others. In case you get confused as you’re writing your loop, check these out: loop and a half, infinite loop, break, sentinel. Remember not to use a break statement as per coding guidelines!
Sample run:
Enter input n: 10
Please choose the operation. Division or Multiplication ‘d’ or ‘m’?: m
Enter the multiplier or divisor that is a multiple of two: 8 0b1010000
Do you want to do another operation? ‘y’ (yes) or ‘n’ (no): y
Enter input n: 16
Please choose the operation. Division or Multiplication ‘d’ or ‘m’?: d Enter the multiplier or divisor that is a multiple of two: 4 0b100
Do you want to do another operation? ‘y’ (yes) or ‘n’ (no): n
Program finished.
4. Busy Euler
Busy Euler was borned in the 20’s. At one weekend, her friends called her and now, somehow, she has to go out with her friends for a coffee. Before that, she needs to calculate the area under curves for her math homework. The problem is: she doesn’t know how to integrate. But she knows how to write while loops. The homework is about calculating the area under the function 𝑥 2, from 𝑥 = 0 to 𝑥 = 1. Linear or maybe parabolic functions are easy but the next questions of her homework𝑓( ) = 𝑥 contain complex functions that make it hard to approximate with simple solutions. She planned to generalize the solution and decided to divide the problem into sub-problems. Therefore, she created small rectangles and as she increased the number of rectangles her approximation became better and better (see Figure below).

Your task is to calculate the area under the function 𝑓(𝑥) = 𝑠𝑖𝑛 (π * 𝑥) + 𝑠𝑖𝑛 (33*π*𝑥) . Below, you can find the initializations she did in her code. You can use them in yours.
x_start : starting point on the x axis x_end : ending point on the x axis step_size : width of the rectangles total_area = 0 : initial total area
(You are supposed to “hard code” the calculation of your specific function and the string ”The function is: x^2” argument in your print method.) Sample run:
The function is: x^2
Parameters: x_start = 0, x_end = 1 step_size = 0.01
Area under “x^2” from 0 to 1: 0.3383500000000004
Sample run:
The function is: sin(PI*x) + sin(PI*3*x)/3
Parameters: x_start = 0, x_end = 1 step_size = 0.01
Area under “sin(PI*x) + sin(PI*3*x)/3” from 0 to 1: 0.7072505742610112
You can use the integral notation of Desmos to check your answers by solving them analytically. It will visualize and at the same time calculate the answer. People are also making art out of linear equations 𝑓(𝑥) = 𝑎𝑥 + 𝑏. You can check an example here.
“It is important to draw wisdom from many different places. If you take it from only one place, it becomes rigid and stale. ” Uncle Iroh
“ Boomerang! You do always come back! ”
Sokka

  • lab4-s2gxjo.zip