Topics:
• Entering, compiling, and running a Java program.
• Using [login to view URL]().
• Java syntax.
• Recognizing syntax and logical errors.
• Arithmetic evaluation
Important Note: All submitted assignments must begin with the descriptive comment block similar
to the one shown below under Part 1, Program #1. It must contain your name and the other information
illustrated. To avoid losing trivial points, make sure this comment header is included in every assignment
you submit, and that it is updated accordingly from assignment to assignment.
Part 1: Program #1 Compile and Run: (5 pts)
Type the following code into a file called TempConverter.java. After the program is entered, compile
and run the application to make sure it works. Once you see it working correctly, answer questions a-e
below. (The comment block shown here is a good example to base yours off of for Part 3, namely
[login to view URL]). You DO NOT have to submit this file.
//***********************************************************
// Name: Faye Navabi
// Title: [login to view URL]
// Description: Computes the Fahrenheit equivalent of a specific Celsius
// value
// Time spent: 20 minutes
// Date: 1/2/2017
//**************************************************************
public class TempConverter
{
// Computes the Fahrenheit equivalent of a specific Celsius
// value using the formula F = (9/5)C + 32.
public static void main (String[] args)
{
//constant variables
final int BASE = 32;
final double CONVERSION_FACTOR = 9.0 / 5.0;
//declare variables
int celsiusTemp = 24; // value to convert
double fahrenheitTemp;
fahrenheitTemp = celsiusTemp * CONVERSION_FACTOR + BASE;
[login to view URL] ("Celsius Temperature: " + celsiusTemp);
[login to view URL] ("Fahrenheit Equivalent: " + fahrenheitTemp);
}
}
There are numerous opportunities for errors in any program, many times in places that seem too simple to
require close attention.
Questions: Introduce the following errors, one at a time, in the program TempConverter.java. Write in
your own words any error messages that the compiler produces. Fix the previous error each time
before you introduce a new one. If no error messages are produced, try to run the code and explain what
happens. If still no errors are produced, explain why.
a. Change TempConverter to tempConverter.
b. Remove the first quotation mark in the first string literal in the first
[login to view URL]( ) statement.
c. Remove the semicolon at the end of the first println ( ) statement.
d. Remove the last brace in the program.
e. Change the variable name fahrenheitTemp to fahrenheit in any one place in the code.
Note: The answers to the 5 questions (a through e) above should be typed in the block of
comments in the [login to view URL] file.
Part 2: Arithmetic Expressions (5pts)
Include the answers to these questions as comments above your code for Part 3 below.
1. Assume that you have a program that contains the following three lines of code. What is the
value stored in the variable mystery after each line in the following code segment (You should
have an answer like a) mystery has the value … b) mystery now has the value ...)?
/*a)*/ int mystery = 7;
/*b)*/ mystery = mystery – mystery / 2;
/*c)*/ mystery = mystery + 1;
2. Assume that you have the following variables declared
int a = 3, b = 10, c = 7;
double w = 12.9, y = 3.2;
What do the following expressions evaluate to in Java?
a) a / b
b) (double) b / a
c) a - b / c
d) w / (int) y
e) y / w
f) b % c / a
g) b % a
Part 3: Programming (10 pts)
Write a Java program called [login to view URL] that will calculate the tip to leave at a restaurant.
Your program must ask the user for the amount of the bill (this could contain decimals), and the
percent they want to tip. The user will enter the percent they want to tip as an integer, e.g. 15
for 15%. Then your program should calculate and display the tip amount and the total bill after
including the tip. To calculate the tip amount, multiply the bill amount times the percent divided
by 100 (be careful of integer division). Make sure to use the following to get full credit:
o Comments explaining the main parts of the code (Getting input, calculating
results, etc)
o Descriptive variable names with appropriate types
o Appropriate indentation between braces
o Use the NumberFormat class to format the output to be display as currency as -
See Example [login to view URL] posted on Blackboard under the tab “Course
Documents” -> “More Code Examples” -> “Currency Format”.
For this and all subsequent assignments, provide a heading (in comments) described above
and demonstrated in Program #1. Make sure your program is called Assignment1.java.
Sample Output
Below is sample output with input in bold. Your output does not need to match directly.
Sample 1
Enter the bill amount: $100.00
What percent would you like to tip: 15
Tip Amount: $15.00
Total amount with tip: $115.00
Sample 2
Enter the bill amount: $15.50
What percent would you like to tip: 10
Tip Amount: $1.55
Total amount with tip: $17.05
Hello
My name is Ahmed, i'm a top rated freelancer.
i worked over 250 projects and over 600 hours doing java projects.
plus i passed freelancer java exam with top 15%
plus i got 2 java certificate 1Z0-803 and 1Z0-804 with 3+ years experience.
small overview of feedback on my profile
"you want to get your projects completed with quality? Hire Ahmed. Highly recommended."
"Thoroughly satisfied with Ahmed, and would recommend him for anyone looking for an excellent java developer!"
"Highly recommended. Very smart and well experience"
looking forward to work with you
Regards
$10 USD en 1 día
4,9 (379 comentarios)
6,5
6,5
12 freelancers están ofertando un promedio de $91 USD por este trabajo
Hello Sir/Mam
NovaSofts Inc. is the leading IT Company of Islamabad Pakistan. We have 9+ years of experience and strong knowledge about programming languages
Java
C++
C
C#
Android
So you don't worry about third party client. We are very much experienced and assure you that we’ll provide quality work. The project bid is negotiable and P.M for sample work. Thanks
Hi
I am a Java developer with over 5 years of experience and two of those are in the UK. Please refer to my profile for more information.
I could get this done within a couple of hours. If you are interested please let me know in the chat.
Cheers,
Saminda
We are proffesional web developer especially in wordpress. We deals in theme development , blog , content , SEO, website optimization and ecomerce site. We have done alot of projects and have experties in it. We can change your psd in interactive wordpress theme or html theme in short spam of time.
Feel free to Contact Us:
Awaiting for your Response to Discuss and Start Work!
Thanks & Regards:
Zibesco