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. This does unfortunately have ads, some of which might be misleading; you do not need to download any extra tools to use this page, just save your file. You may also use any other java IDE you prefer, this is the most convenient one I found.

More about using the IDE for this course.

About Java:

About the Java IDE:

About this assignment:


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, but it is not complete. Edit this code and 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 MainA
{
    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 an int

        // 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 now " + firstNum);
        System.out.println("second is now " + secondNum);

// main code ends (still need the below two lines!!)        
    }
}


Part B

The following example code shows some more variable types in java. Using this example as a model, write new java code that:
// This code sets things up for a general java program with input
import java.util.Scanner;
public class MainB
{
    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

        // this is overkill, but shows you how to do a constant in Java using keyword final
        final double NUMCOUNT = 2.0;
        
        // do math and print the result
        double average = (a + b) / NUMCOUNT;
        System.out.println(userName + " your average is " + average);



// main code ends (still need the below two lines!!)  
    }
}