Homework 2
We will use Java to do a few programming tasks. Here is an online Java IDE -- an editor that also lets you
run code right in the browser. At the top is the editor, below that is a Run button, and then below that the output.
You can edit the code in the editor and run it as many times as you want.
About Java:
- Most lines need to end in a semicolon, but conditionals do not.
- This is java, not our pseudocode, so some things we use in class, like prompt, are not available.
I have provided examples of all the syntax you should need to be using.
About the Java IDE:
- You have to click Run again each time you want it to run your code to test changes.
- At top left there is a save (floppy) button! This page is pretty good but not perfect, so save often and don't lose your work!
- It WILL NOT SAVE your code if you close the browser, so you need to save!!!
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off using the settings (gears) button
at top right and unchecking Autocomplete at the bottom of the list, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
doesn't show up til a later line.
About this assignment:
- The editor page starts off with example code in it. REPLACE the example code with the code in the assignment. This means to
DELETE ALL of the code in the editor, and then paste in my example code. ALL.
- When you do a different part, you will need to again REPLACE ALL of the code (you can open more tabs with copies of the IDE in your browser) so make sure you SAVE.
- Attach all your java files to a single submission in blackboard, not multiple submissions.
The following example code shows using conditionals in java.
// This code sets things up for a general java program with input
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// done with setup, main code starts here
// print and then read in a double from the user
System.out.println("what is a?");
double a = scan.nextDouble(); // example of declaring and reading in on same line - note specific code for double
// print and then read in from the user
System.out.println("what is b?");
double b = scan.nextDouble();
// if-else if-else structure - note the parens()
// in java we use { instead of then -- and no semicolon; there!!
if (a > b) {
System.out.println(a + " is biggest");
} else if (b > a) {
System.out.println(b + " is biggest");
} else {
System.out.println(a + " and " + b + " are the same");
} // in java we use } instead of endif
// do math and print the result
double average = (a + b) / 2;
System.out.println("The average is " + average);
// print and then read in a String from the user
System.out.println("Are you a ghost?");
// the type String is capitalized in this language
String userWord = scan.next(); // note specific code for String
// strings in java use a special method called .equals() to check equality
// this checks if userWord is equal to "Yes"
if ( userWord.equals("Yes") ) {
System.out.println("Well boo to you too");
} else {
System.out.println("Phew, I was scared for a minute there!");
}
// main code ends (still need the below two lines!!)
}
}
Using this code as a model, write code (each should be a separate program) to
- Welcome the user to the zoo. Ask the user their age. Kids under 5 get in free, under 18 cost $5, adults cost $10, and over 65 cost $5. Also ask them if they want a ticket to the Vulture Ballet performance, which costs (for anyone) $8. Then tell them their total cost.
- Ask the user if they want to do a square, or a rectangle. Depending on which one they want, ask them for the
necessary information and print the correct area for the shape. [EC+20] Give them four options: area of a square, area of a rectangle, volume of a cube, or volume of a cylinder.