• Home
  • Blog
  • 5 Tasks with simple Java coding.

5 Tasks with simple Java coding.

0 comments

Program must look exactly like the sample runs.

Task 1

File: HW2_Cities.java
Implement the program discussed in class (on paper) on Monday, Feb 3. See the Daily webpage for the scan of what was written in class and watch the Lecture video from that day if needed.
You can get however much help on this task or work together with another classmate. But this (collaboration and extra help allowed) applies only to this task, not the other tasks of this and future homeworks.
Use nextLine() to be able to read cities with 2 words in their name, but place another nextLine() after reading the number for the area to get rid of the new line left over from reading with any of nextDouble() or nextFloat().

---------Sample run 1:
Enter city1: New York
Enter area1: 302.6
Enter city2: Arlington
Enter area2: 99.69
Enter city3: Dallas
Enter area3: 385.8
------------------------------
|City           |        Area|
------------------------------
|New York       |      302.60|
------------------------------
|Arlington      |       99.69|
------------------------------
|Dallas         |      385.80|
------------------------------

-------- Sample run 2:
Enter city1: New York
Enter area1: 302.6
Enter city2: Arlington
Enter area2: 99.69
Enter city3: Dallas
Enter area3: 385.8
------------------------------
|City           |        Area|
------------------------------
|New York       |      302.60|
------------------------------
|Arlington      |       99.69|
------------------------------
|Dallas         |      385.80|
------------------------------

Task 2

File: HW2_Time.java
Write a program that computes the time to arrive to destination. It asks for the distance and the car speed. It will assume that the car travels at constant speed at all time.
Sample run 1:

This program will compute the travel time.
Enter the distance (miles): 105
Enter the car speed (miles/hour): 75
Travel time: 1.40
or: 1 hour and 23 minutes

Sample run 2:

This program will compute the travel time.
Enter the distance (miles): 150
Enter the car speed (miles/hour): 60
Travel time: 2.50
or: 2 hour and 30 minutes

Hint: to go from 0.5 hours to minutes you can multiply by 60: 0.5*60 = 30.


Task 3

File: HW2_BankTable.java
Write a program that simulates 2 bank transactions. The user will enter: the original amount in the bank, the money for transaction 1 and the money for transaction 2.
If a transaction has a negative number it means that money was removed from bank (an withdrawal).
If a transaction has a positive number it means that money was deposited in the bank.
Hint: if you add a negative number you are effectively subtracting. E.g. 100 + (-10) = 90 (so if you just add the transaction, it will do the correct computation for both positive/deposit and negative/removal transactions)
Print the data as a table. Use System.out.printf and reserve a minimum number of spaces to ensure the table stays aligned. Count the – to see how many spaces to reserve.

This program will simulate 2 bank transactions.
Enter the original amount of money in the bank: 100.03
Enter the 1st transaction: 1000.2
Enter the 2nd transaction: 36
---------------------------
| Transaction |  Account  |
|             |    100.03 |
|     1000.20 |   1100.23 |
|       36.00 |   1136.23 |
---------------------------

Sample run 2:

This program will simulate 2 bank transactions.
Enter the original amount of money in the bank: 10023
Enter the 1st transaction: -56
Enter the 2nd transaction: 90.3
---------------------------
| Transaction |  Account  |
|             |  10023.00 |
|      -56.00 |   9967.00 |
|       90.30 |  10057.30 |
---------------------------

Sample run 3:

This program will simulate 2 bank transactions.
Enter the original amount of money in the bank: 30
Enter the 1st transaction: 7
Enter the 2nd transaction: -5
---------------------------
| Transaction |  Account  |
|             |     30.00 |
|        7.00 |     37.00 |
|       -5.00 |     32.00 |
---------------------------

Task 4

File: HW2_Substitute.java
Write a program that asks the user for some text and then it replaces 2 pieces of it (substrings).
Hint: The special case when one part to be substituted is at the beginning does NOT need any special code. Whatever works for text in the middle, works for that as well.
Remember that you cannot ‘substitute’ in a string. You have to build a new string with the pieces needed form the old strings and the new pieces.
Hint: Solve this problem by first ‘substituting’ the first piece, and then, in that new text, substitute the second piece. E.g. for the sample run 1 below the final text was produced following these steps:
start with string: Wait. where is the car?
produce a new string that has goes instead of is: Wait. where goes the car?
using the last text, produce another one that has !!!!!!!!! instead of . : Wait!!!!!!!!! where goes the car?
you now have the final text. You can print it. Suggestion: Since you do not need to reprint the original text from the user, you can save all the new texts you produce in that same variable. (It is also ok to use new variable names.)

Sample run 1 (italic font shows user input):
Enter the text: Wait. where is the car?
Enter existing word 1: is
Enter new word 1: goes
Enter existing word 2: .
Enter new word 2: !!!!!!!!!
The new text is: Wait!!!!!!!!! where goes the car?

Sample run 2:
Enter the text: cats dogs house farm 36521 hdj
Enter existing word 1: cats
Enter new word 1: h
Enter existing word 2: ar
Enter new word 2: MMMM
The new text is: h dogs house fMMMMm 36521 hdj

Task

File: HW2_Digits.java
Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, …

Enter an integer of exactly 3 digits(e.g. 538): 319
9
1
3

Hint: consider what you get from these operations:
319%10
319/10
31%10

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}