Homework 1
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.
Part A
The following code in Java creates two variables, prints them, and then creates a third variable.
It should then swap the values of the first two variables, using the third as temporary space,
and then print the two variables again. Replace the 111, 222, and 333 in this code correctly so that the
values are swapped.
// 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
// declare variables
// pay attention to the semicolons;
int firstNum;
int secondNum;
// print and then read in from the user
System.out.println("what is the first number?");
firstNum = scan.nextInt(); // reading in - note specific code for reading in String
// print and then read in from the user
System.out.println("what is the second number?");
secondNum = scan.nextInt();
// print before swap
System.out.println("first is " + firstNum);
System.out.println("second is " + secondNum);
// extra variable
int temp;
// FIX THIS TO SWAP
temp = 111;
firstNum = 222;
secondNum = 333;
// print after swap
System.out.println("first is " + firstNum);
System.out.println("second is " + secondNum);
// main code ends (still need the below two lines!!)
}
}
Part B
The following example code shows some more variable types in java. Based on what is shown in the example, Write
java code that gets from the user what type of animal, the the price per animal, and how many of the animal
the user is buying. Assuming 15% tax, print them a nice summary of what they are buying, with the total.
// 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();
// print and then read in a String from the user
System.out.println("What is your name?");
// the type String is capitalized in this language
String userName = scan.next(); // note specific code for String
// do math and print the result
double average = (a + b) / 2;
System.out.println(userName + " your average is " + average);
// main code ends (still need the below two lines!!)
}
}