Question: Just need some help, using java on intellij - big question but the code itslef isnt big- thanks Task 1/4: Decisions Background: Simple Flow of
Just need some help, using java on intellij - big question but the code itslef isnt big- thanks
Task 1/4: Decisions
Background: Simple Flow of Control
if Statement, if ... else Statement In the programs you have seen so far, we have had a list of statements, which were executed in the order that they were written in your program (the .java file). In more complicated programs, you may need to change the order in which statements are executed. The order of execution for statements in your program is referred to as flow of control. Lets look at one example. Suppose you are organising an event that costs $12 for everyone older than 8 and $6 for anyone 8 years or younger. One way to do this is to say the ticket is $12, unless you are 8 or younger, then it is $6. In this case, you can write:
| pseudocode | java equivalent | |
| --------------------------------------- | ----- | ----------------------- |
| ticket = 12 | double ticket = 12.0; | |
| age | double age; | |
| if age is less than or equal to 8 then | if (age <= 8) | |
| ticket = 6 | ticket = 6; |
There is another way to do this.
| pseudocode | java equivalent | |
| --------------------------------------- | ----- | ----------------------- |
| if age is less than or equal to 8 then | if (age <= 8) | |
| ticket = 6 | ticket = 6; | |
| otherwise | else | |
| ticket = 12 | ticket = 12; |
Both of these do the same thing. In both cases, you will change the flow of execution when you reach the statement; age is 8 or younger. If that statement happens to be true, i.e., age is 8 or younger, then the value of ticket will change to $6, otherwise, you will go with its initialised value of $12. In general, the statement in the parentheses is either TRUE or FALSE. Depending on that being true or false, you will change the flow of execution of the statements in the program. In order for your program to decide about the flow of the execution, it uses a comparison operator. Examples of comparison operators are:
equal to, = , which will be written as == (2 =s) in java, with a general form of: statement1 == statement2. Example: y == x + 1
not equal to, , which will be written as != in java, with a general form of: statement1 != statement2. Example: y != x + 1
less than, <, which will be written as < in java, with a general form of: statement1 < statement2. Example: y < x + 1
less than or equal to, , which will be written as <= in java, with a general form of: statement1 <= statement2. Example: y <= x + 1
greater than, >, which will be written as > in java, with a general form of: statement1 > statement2. Example: y > x + 1
greater than or equal to, , which will be written as >= in java, with a general form of: statement1 >= statement2. Example: y >= x + 1
OR, which will be written as || (2 of the |s) in java, with a general form of: statement1 || statement2. Which may be True when EITHER one of the two statements are TRUE.
AND, which will be written as && (2 of the &s) in java, with a general form of: statement1 && statement2. Which may be True ONLY when both statements are TRUE.
Now that you have learned about changing the flow of control, lets look at the program Ticket.java that asks users to enter an age and displays the cost of the ticket based on the criteria that was given in the prelude (first example). Try the other method to make sure it works.
Once you are confident that the code works as expected change it so that it displays $6 for people who are 8 years old or younger OR 65 years or older.
this is what im given as a starter - for some reason i just cant get it correct
public class Ticket { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); double ticket = 12; double age; System.out.println("Please enter your age"); age = keyboard.nextDouble(); if (age <= 8) { ticket = 6; } System.out.println("Your ticket costs " + ticket); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
