Welcome to Computer Programming

 

HWJ3 - Using the scanner class: (This counts as 9 points)

Start with the code below and use it to create the 9 methods described.

import java.util.Scanner;
public class HWJ3_Scanner
{
    public static void testOfScannerClass()
    {
        //You need to initialize the scanner in order to use it
        Scanner scan=new Scanner(System.in);
        //test of reading Strings
        System.out.println("Type in a string");
        String theString=scan.nextLine();
        echo("In case you forgot, you just typed " + theString);

        //test of reading int
        System.out.println("Type in an integer");
        int theInt=scan.nextInt();
        echo("In case you forgot, you just typed " + theInt);

        //test of reading double
        echo("Type in a decimal");
        double theDouble=scan.nextDouble();
        echo("In case you forgot, you just typed " + theDouble);
    }

    //Create a method that will ask your name and age and then
    // find how many years older mr borland is then you (im 40)
    //it will spit out Mr Borland is 22 yrs older than Luke
    public static void ageDiff()
    {
        Scanner scan=new Scanner(System.in);
        echo ("ask and do stuff now");
    }

    //Create a method that will convert meters to centimeters and also inches.
    //It will ask for meters and then print out the number of cms and inches (there are 39.37 inches in meter)
    //So if they put in 1.5 meters, it will say that is 150 cms or 59.06 inches
    public static void convertMeters()
    {
        Scanner scan=new Scanner(System.in);
        echo ("ask and do stuff now");
    }

    //Ask for a weight in pounds convert to kilograms.
    //it would say 180 pounds is 81.6466266 kilograms (convert by pounds*.453592
    public static void convertToKilograms()
    {
        //You will need the following line in all methods in order to initialize the scanner.
        Scanner scan=new Scanner(System.in);
         echo ("ask and do stuff now");

   }

    //Ask the user for their dogs name and age.  You will then convert the dogs age in to years by multiplying by 7 and then saying:
    //Your dog Lassie is 42 yrs old.
    public static void findDogsAge()
    {
        //You will need the following line in all methods in order to initialize the scanner.
        Scanner scan=new Scanner(System.in);
        echo ("ask and do stuff now");
    }

    //Ask the user for their first and last name and then they will say:
    //Jeff Borland, there are 4 letters in your first name and 7 in your last.
    public static void findLettersInName()
    {
        //You will need the following line in all methods in order to initialize the scanner.
        Scanner scan=new Scanner(System.in);
         echo ("ask and do stuff now");

    }

    //Ask the user for 2 numbers and spit out the average
    public static void findAvg()
    {
        //You will need the following line in all methods in order to initialize the scanner.
        Scanner scan=new Scanner(System.in);
         echo ("ask and do stuff now");

    }

    //Ask the user for their height in feet and inches (read both things seperately).
    // Then spit out You are 75 inches tall.
    public static void findHeight()
    {
        //You will need the following line in all methods in order to initialize the scanner
        Scanner scan=new Scanner(System.in);
        echo ("ask and do stuff now");

    }

    /**************************************************************************/
    //Advanced Students - do __ of below (look at notes on if statements, loops)
    //Ask the user for a number of miles for a taxi ride and then ask for
    // cost per mile and pickup cost.
    //Then spit out total charge (which is #of miles *cost per mile + pickup cost
    public static void findTaxiCost()
    {
    }

    //This will ask the user for the speed he was going and then spit out
    //the cost of a ticket (from 0-65 - $0, 66-74 - $85, 75-100 - $200 101+ - $1000
    public static void findTicket()
    {
    }

    //This will find out the number of people who attended each block of the day
    //Ask the user 4 times (for each block) how many people attended
    //Then spit out the total
    //you must use a for loop
    public static void find4BlockTotal()
    {
    }

    //This will ask the user how many games they played, then it will find the winning percent
    //So the user might say 3, and the program will ask them 3 times if they won
    //So they might say y,n,n and then it will spit out you won 33.3% of the time
    public static void findWinningPrct()
    {
    }

    //create a method that will keep asking for a grade until the user hits -1.  They will then
    //find the average of those grades
    // you will need to use loops
    public static void findAverage()
    {
    }

    //The following 2 are just for printing faster
    public static void echo (String x){
       System.out.println(x);
    }

    public static void echo (int x){
        System.out.println(x);
    }
}

Advanced Students -notes on if statements, loops)