> Question with coding in java.?

Question with coding in java.?

Posted at: 2014-12-18 
If-else statements are basically like a switch. The statement checks if a certain condition is met. If so, a certain path is followed. If not, another condition is checked. If that's true, another path is followed and so on. For example,

if (x == 3) { y = 1; }

else if (x == 2) { y = 8; }

else { y = 0; }

In the example above, if x is equal to 3, y is set to 1. If not, then if x is equal to 2, y is set to 8. If both these conditions are false, y is set to 0.

For your problem, there are two different functions that determine the cost of the call. Under or at two minutes, the function is cost = 1.15. Over two minutes, the function becomes cost = 1.15 + 0.50 * (t - 2). In other words, the initial cost plus any additional time (total time minus time covered by initial cost) at the 50-cent rate. Therefore, the "if" condition should check if the time is under or at two minutes, and provide the first function described above. The "else" condition should provide the second function, in case the time is more than two minutes. Below is a main() method that does this.

public static void main(String[] args)

{

Scanner reader = new Scanner(System.in); // This object gets user input

System.out.println("Enter the length of the call in minutes: ");

double time = reader.nextDouble(); // Read the next double entered by the user

double cost;

if (time <= 2) // If the time is less than or equal to 2,

{

cost = 1.15;

{

else // If the time is greater than 2,

{

cost = 1.15 + 0.5 * (time - 2);

{

System.out.println("The cost of the call is: $" + cost); // Example: "The cost of the call is $3.00"

}

An alternate method might look like this:

public static void main(String[] args)

{

Scanner reader = new Scanner(System.in); // This object gets user input

System.out.println("Enter the length of the call in minutes: ");

double time = reader.nextDouble();

double cost = 1.15; // Since the cost is at least 1.15 either way, might as well initialize it that way.

if (time > 2) // Only in this case would it be more.

{

cost = 1.15 + 0.5 * (time - 2);

{

System.out.println("The cost of the call is: $" + cost);

}

I would recommend running the above code (provided I didn't put a typo in there) if you think it would help your understanding of if-else statements to work with an example problem. If you copy this directly and turn it in as homework, some differences in style and variable names might give you away. Be sure and check what kind of naming conventions your particular textbook uses. Hope this helps, and good luck :).

This is considered AP?

You simply make a constant (final) called AdditionalMinutes or whatever and set it to .5;

Create a double variable called Cost and set it to 1.15

Prompt the user and ask how long the length of the call was, and create a variable to store that in. Call it

CallLength

if (CallLenght <= 2.0)

{

system.out.println(Cost);

}

else

{

system.out.println(Cost + (CallLength - 2.0) * AdditionalMinutes));

}

In my AP computer science class we are supposed to code the following program : A 2-minute phone call to Lexington, Virginia, costs $1.15. Each additional minute costs $0.50. Write a program that takes the total length of a call in minutes as input and calculates and displays the cost.

this code involved the implementation of if-else statements which I cannot figure out how to do. Could somebody explain how I would go about writing this program. Mind you I am a novice only a few weeks into the class.