辅导案例-COMP 1130

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
1


COMP 1130
Computer Programming I
Midterm 2 Assignment A – 26 marks

Instructions
• questions for students with student numbers ending in 0,1,2,3,4.
− if the incorrect assignment is submitted, this is a mark of zero (0)
− you can use previous exercises & solutions, lectures notes, and the internet…but the work is your own!

• complete the questions below, with marks for each distributed against,
− providing valid solutions, that are well-written and for programs that are efficient and well-structured
− programs with proper & adequate documentation (program header & comments), and well-named identifiers
− when required, clear & informative user interaction (clear prompts)
− sufficient output capture showing/proving program solutions solve the given problem

• some questions may have multiple parts, and with marks given across all parts included in the solution
(partial marks given for parts completed…output capture still required to show functionality for your completed work)


2
[5 marks] Question 1
You are working on a programming team that is developing a control system for an intelligent-driving vehicle. The software
controlling the vehicle is designed with Object-Oriented Programming concepts, and one of the classes encapsulates the
“sensors” within the vehicle.

But something bad happened. Due to a disk error, some of the CarSensor class source code (Java file) was damaged!
Fortunately, a test program for the class was rescued with no errors, and a copy of the output capture (see below).

Your task: fix the CarSensor class, so the test program (runs with no changes), to produce the same results.
Make no changes to the test program, but only fix the CarSensor class.

Running the TestCarSensor program, the output is:

none limits: 0 to 0 noscale, current value: 0, error range: 0 to 0
special sensor limits: -5 to 5 units, current value: 0, error range: 0 to 0

temperature sensor limits: -50 to 300 C, current value: 0, error range: 0 to 0
speed sensor limits: 0 to 200 km/h, current value: 0, error range: 0 to 0
speed sensor 2 limits: 0 to 200 m/h, current value: 0, error range: 0 to 0

special sensor limits: -5 to 5 units, current value: 0, error range: 0 to 0
special sensor limits: -5 to 5 units, current value: 0, error range: 0 to 0
special sensor limits: -10 to 10 blah, current value: 0, error range: 0 to 0

speed sensor limits: 0 to 200 km/h, current value: 0, error range: 0 to 0
=
speed sensor 2 limits: 0 to 200 m/h, current value: 0, error range: 0 to 0
temperature sensor limits: -50 to 300 C, current value: 0, error range: 0 to 0
!=
special sensor limits: -5 to 5 units, current value: 0, error range: 0 to 0


speed sensor limits: 0 to 200 km/h, current value: 22, error range: 22 to 22
speed sensor limits: 0 to 200 km/h, current value: 22, error range: 22 to 22


speed = speed true
speed = speed2 false

temperature sensor limits: -50 to 300 C, current value: 0, error range: 0 to 0
temperature sensor limits: -50 to 300 C, current value: 25, error range: 20 to 30
error range: 20 to 30


Open the CarSensor_code.txt file, which includes the source code for both TestCarSensor and CarSensor classes.

Submission:
- your fixed CarSensor class Java file (there is no need to submit the test program)
o for things you are unable to fix, you will have to comment out code so it compiles & runs
- an output capture of running the provided test program, with the output matching the above


3
[3 marks] Question 2

Refer to the definition of the Coin class discussed during lecture.

Write a new method .flipCoins(), that flips two (2) coins: the current coin and the coin passed as a parameter. The method
returns the integer addition of the flipSides of both coins.

Example, as if used in the CointTester program class:

Coin bobCoin = new Coin(); // create Coin object [using Coin() constructor]
Coin aliceCoin = new Coin(6); // create Coin object with 6 sides
int combinedFlip = 0; // combined flip value of two coins

combinedFlip = bobCoin.flipCoins(aliceCoin); // flip both coins, returned combined
System.out.println ("Flip of two coins: " + combinedFlip);

Submission: In the output capture document, include,
- only your definition of the method .flipCoins(), not the entire Coin class nor CoinTester program class
- output capture of running the above example at least three (3) times.


4
[8 marks] Question 3
Assume the only console output method available is display(c), shown below, which you include in your solution.
This means you are not permitted to use System.out.print(), System.out.println(), nor System.out.printf() in your methods.

public static void display (char c)
{
System.out.print (c);
}

Implement the following methods in a program class, that has a main() method to simply test each methods. Apply the concept
of overloading, where best, and that the methods can call themselves to reduce repeating the same code.


Part 1: Common output methods:
- display (String str) – displays all the characters in the String str
- displayln (String str) – displays all the characters in the String str, following by newline character ('\n')
- display (int val) – displays the integer in variable val
- display (double val) – displays the double in variable val
- displayln () – displays only a newline character


Part 2: Special output methods:
- displayRepeat (String str, int rep) – displays the String str, repeated as many times indicated in variable rep
- displayTriangle(String str)) – displays all the characters in the String str, in a triangle form,
ex: displayTriangle("pies") shows:
p
ii
eee
ssss

Part 3: Include the methods in your own utility class
- name the utility class whatever you want ( Util, Special, or own name, such as: YanniUtil )
- move the methods you wrote into your utility class
- adjust your test program class to call the methods from your utility class


Submission:
- include your solution, to the degree it is completed: as the single program file, or test program with utility class
- an output capture of running your test program
5
[10 marks] Question 4
Write an application that converts temperatures between the Fahrenheit and Celsius scales

The interesting aspect of the application is that the input controls which direction (i.e., calculation) to calculate. The user is
prompted to enter both the temperature and the scale it is in (as the words “Cel” or “Fahr” in upper, lower, or mixed case);
if the scale is invalid, display an error message. The result is the conversion in the alternate scale (see examples).

https://www.mathsisfun.com/temperature-conversion.html
Formulas: F → C: (F – 32) * (5/9) = C
C → F: C * (9/5) + 32 = F

Consider the examples of repeated runs of the program (user input is in highlight):


** this is correct: -40 F = -40 C


(Note: output results to 2 decimal places)

Part 1
Complete a well-structured & well-written program as described above. You can include methods that help reduce the complexity
of the main() method. Include commenting, and test with sufficient values to ensure the program works as expected (and consider
“simple” values, such as 0.0, 1.0, and -40.0, in both directions, to clearly show calculations are correct).
Capture the output of running this program with your test values.

Part 2
Save as a new application name, and adjust the program to perform an “automatic” and random calculation:
- Rather than ask the user, the program randomly selects: a) temperature (between -10 and +10); and b) scale (“Fahr” or “Cel”)
- Display the random temperature and scale, perform the appropriate calculation, and display the result
Capture the output of running this program 5 or 6 times.

Part 3
Adjust the program from Part 2, with the following,
- Prompt the user for the “number of random calculations” (# of iterations)
- Use this value to perform the “automatic” calculation program that many times, generating a new random temperate and scale
for each iteration
Capture the output of running this program with a couple of different values for # or iterations.

Submission: The two program submissions and output captures required: Part 1, and Parts 2 & 3
Temperate and scale: 10.10 Cel
Conversion: 50.18 Fahrenheit


Temperate and scale: 10.10 Flop
Invalid scale
Temperate and scale: -40.0 FAHR
Conversion: -40.00 Celsuis


Temperate and scale: 0 fahr
Conversion: -17.78 Celsius



欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468