Homework 1


We will use Java to do a few programming tasks.

Quick ref about Java for 115.


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. Run and test that your solution works.

import java.util.Scanner; // needed for input
public class Hwk1A {
    public static void main(String[] args) {

      // main code starts here   

        // declare variables
        // pay attention to the semicolons;
        int firstNum;
        int secondNum;
        
        // read in an int from the user
        // promptInt is a method I wrote, code is below the main
        firstNum = promptInt("what is the first number?"); // note specific method for reading in an int

        // print and then read in from the user
        secondNum = promptInt("what is the second number?");

        
        // print before swap  System.out.println is java's printing method
        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);

        
    } // end of main code
    
     // utility for reading in integer input
    public static int promptInt(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        int response = scan.nextInt();
        return response;
    }
    
} // end of class structure ends the program


Part B

The example code below shows some more variable types in java. Using this and the above example as a model, replace the main code in MainB with new java code that:

import java.util.Scanner;  // needed for input
public class Hwk1B {
    public static void main(String[] args) {

        // main code starts here   

        // prompt the user for a double
        double a = promptDouble("What is a?");

        // prompt the user for a double
        double b =promptDouble("What is b?"); 

        // prompt the user for a  String
        // the type String is capitalized in this language
        String userName = promptString("What is your name");

        // 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);


    } // end of main code
    
    /*    Simple Utility methods that prompt the user and return input
          If you enter the wrong type of input, the program will end
          with an error!
    */
    
    // utility for reading in integer input
    public static int promptInt(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        int response = scan.nextInt();
        return response;
    }
    
    // utility for reading in double input
    public static double promptDouble(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        double response = scan.nextDouble();
        return response;
    }
    
    // utility for reading in String input
    public static String promptString(String promptText) {
        System.out.print(promptText + " ");
        Scanner scan = new Scanner(System.in);
        String response = scan.nextLine();
        return response;
    }

    
} // end of class ends the whole program