Famcraft

Author Topic: Coding Fun Part 2  (Read 3789 times)

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Coding Fun Part 2
« Opened on April 10, 2014, 10:09:04 PM »
Runner.java

public class Runner
{
    String Runner1, Runner2, Runner3;
    private double Time1;
    private double Time2;
    private double Time3;
   
    public void setRunner1(String r1)
    {
        Runner1 = r1;
    }
   
    public void setRunner2(String r2)
    {
        Runner2 = r2;
    }
   
    public void setRunner3(String r3)
    {
        Runner3 = r3;
    }
   
    public String getRunner1()
    {
        return Runner1;
    }
   
    public String getRunner2()
    {
        return Runner2;
    }
   
    public String getRunner3()
    {
        return Runner3;
    }
   
    public double getTime1()
    {
        return Time1;
    }
   
    public double getTime2()
    {
        return Time2;
    }
   
    public double getTime3()
    {
        return Time3;
    }
}


RunnerDemo.java

import java.util.Scanner;

public class RunnerDemo
{
    public static void main(String[] args)
    {
        String Runner1, Runner2, Runner3;
        double Time1;
        double Time2;
        double Time3;
        int status = -1;
       
        Scanner keyboard = new Scanner(System.in);
       
        System.out.print("What is the name of the first runner? ");
        Runner1 = keyboard.nextLine();
        System.out.print("What is the name of the second runner? ");
        Runner2 = keyboard.nextLine();
        System.out.print("What is the name of the third runner? ");
        Runner3 = keyboard.nextLine();

        System.out.print("What was the time for " + Runner1 + "? ");
        Time1 = keyboard.nextDouble();
        System.out.print("What was the time for " + Runner2 + "? ");
        Time2 = keyboard.nextDouble();
        System.out.print("What was the time for " + Runner3 + "? ");
        Time3 = keyboard.nextDouble();
       
If statements go here to order stuff to order input from quickest time to slowest time

    }
}
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline mathfreak2

  • Retired
  • Hero Member
  • *
  • Posts: 941
  • integral from 0 to 2sqrt(21) of x wrt x = 42
    • View Profile
Re: Coding Fun Part 2
« Reply #1 on April 10, 2014, 10:29:23 PM »
Runner.java

public class Runner
{
    String runner; \\remember the tradition of variables always being lowercase
    private double time;
   
    public Runner(String r, double t)
    {
        runner = r;
        time = t;
    }
    public String getRunner()
    {
        return runner;
    }
    public double getTime()
    {
        return time;
    }
}


RunnerDemo.java

import java.util.Scanner;

public class RunnerDemo
{
    public static void main(String[] args)
    {
        String runner1, runner2, runner3;
        double time1;
        double time2;
        double time3;
        int status = -1;
       
        Scanner keyboard = new Scanner(System.in);
       
        System.out.print("What is the name of the first runner? ");
        runner1 = keyboard.nextLine();
        System.out.print("What is the name of the second runner? ");
        runner2 = keyboard.nextLine();
        System.out.print("What is the name of the third runner? ");
        runner3 = keyboard.nextLine();

        System.out.print("What was the time for " + Runner1 + "? ");
        time1 = keyboard.nextDouble();
        System.out.print("What was the time for " + Runner2 + "? ");
        time2 = keyboard.nextDouble();
        System.out.print("What was the time for " + Runner3 + "? ");
        time3 = keyboard.nextDouble();

        Runner run1 = new Runner(runner1, time1);
        Runner run2 = new Runner(runner2, time2);
        Runner run3 = new Runner(runner3, time3);
       
        if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
       {
              System.out.println(run1.getRunner());
              if (run2.getTime() > run3.getTime())
                   System.out.println(run2.getRunner());
              else
                   System.out.println(run3.getRunner());
        }
        if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
       {
             System.out.println(run2.getRunner());
             if (run1.getTime() > run3.getTime())
                   System.out.println(run1.getRunner());
             else
                   System.out.println(run3.getRunner());
        }
        if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
       {
            System.out.println(run3.getRunner());
            if (run1.getTime() > run2.getTime())
                   System.out.println(run1.getRunner());
            else
                   System.out.println(run2.getRunner());
       }
    }
}

This, or some variant, should work. I will note that there are a few easier ways using ArrayLists, but I don't know whether or not you have gotten that far into learning java yet, so I used the brute force method with the if statements. I hoped this helped! ^^
<Thoatt> I was very much into fluid dynamics
(SrMod: ~Rowan) but your opinion on that was... fluid?
<Thoatt> up to ferrofluid in magnetic field dynamics
(SrMod: ~Rowan) that sounds like an attractive field
(SrMod: ~Rowan) I'm going hard on the puns right now XD

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Re: Coding Fun Part 2
« Reply #2 on April 10, 2014, 10:55:10 PM »
import java.util.Scanner;

public class RunnerDemo
{
    public static void main(String[] args)
    {
        String runner1, runner2, runner3;
        double time1;
        double time2;
        double time3;
       
        Scanner keyboard = new Scanner(System.in);
       
        System.out.print("What is the name of the first runner? ");
        runner1 = keyboard.nextLine();
        System.out.print("What is the name of the second runner? ");
        runner2 = keyboard.nextLine();
        System.out.print("What is the name of the third runner? ");
        runner3 = keyboard.nextLine();

        System.out.print("What was the time for " + runner1 + "? ");
        time1 = keyboard.nextDouble();
        System.out.print("What was the time for " + runner2 + "? ");
        time2 = keyboard.nextDouble();
        System.out.print("What was the time for " + runner3 + "? ");
        time3 = keyboard.nextDouble();

        Runner run1 = new Runner(runner1, time1);
        Runner run2 = new Runner(runner2, time2);
        Runner run3 = new Runner(runner3, time3);
       
        if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
       {
              System.out.println(run1.getRunner() + " was in first with a time of " + run1.getTime());
              if (run2.getTime() > run3.getTime())
                   System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
                   System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
              else if (run2.getTime() < run3.getTime())
                   System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
                   System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
        }
        if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
       {
             System.out.println(run2.getRunner() + " was in first with a time of " + run2.getTime());
             if (run1.getTime() > run3.getTime())
                   System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
                   System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
             else if (run1.getTime() < run3.getTime())
                   System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
                   System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
        }
        if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
       {
            System.out.println(run3.getRunner() + " was in first with a time of " + run3.getTime());
            if (run1.getTime() > run2.getTime())
                   System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
                   System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
            else if (run1.getTime() < run2.getTime())
                   System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
                   System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
       }
    }
}



Getting the 'else' without 'if' error
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline mathfreak2

  • Retired
  • Hero Member
  • *
  • Posts: 941
  • integral from 0 to 2sqrt(21) of x wrt x = 42
    • View Profile
Re: Coding Fun Part 2
« Reply #3 on April 10, 2014, 11:00:10 PM »
import java.util.Scanner;

public class RunnerDemo
{
    public static void main(String[] args)
    {
        String runner1, runner2, runner3;
        double time1;
        double time2;
        double time3;
       
        Scanner keyboard = new Scanner(System.in);
       
        System.out.print("What is the name of the first runner? ");
        runner1 = keyboard.nextLine();
        System.out.print("What is the name of the second runner? ");
        runner2 = keyboard.nextLine();
        System.out.print("What is the name of the third runner? ");
        runner3 = keyboard.nextLine();

        System.out.print("What was the time for " + runner1 + "? ");
        time1 = keyboard.nextDouble();
        System.out.print("What was the time for " + runner2 + "? ");
        time2 = keyboard.nextDouble();
        System.out.print("What was the time for " + runner3 + "? ");
        time3 = keyboard.nextDouble();

        Runner run1 = new Runner(runner1, time1);
        Runner run2 = new Runner(runner2, time2);
        Runner run3 = new Runner(runner3, time3);
       
        if (run1.getTime() > run2.getTime() && run1.getTime() > run3.getTime())
       {
              System.out.println(run1.getRunner() + " was in first with a time of " + run1.getTime());
              if (run2.getTime() > run3.getTime())
              {
                   System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
                   System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
              }
              else
                   System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
                   System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
              }
        }
        if (run2.getTime() > run1.getTime() && run2.getTime() > run3.getTime())
       {
             System.out.println(run2.getRunner() + " was in first with a time of " + run2.getTime());
             if (run1.getTime() > run3.getTime())
             {
                   System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
                   System.out.println(run3.getRunner() + " was in last with a time of " + run3.getTime());
             }
             else
             {
                   System.out.println(run3.getRunner() + " was in second with a time of " + run3.getTime());
                   System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
             }
        }
        if (run3.getTime() > run1.getTime() && run3.getTime() > run2.getTime())
       {
            System.out.println(run3.getRunner() + " was in first with a time of " + run3.getTime());
            if (run1.getTime() > run2.getTime())
           {
                   System.out.println(run1.getRunner() + " was in second with a time of " + run1.getTime());
                   System.out.println(run2.getRunner() + " was in last with a time of " + run2.getTime());
           }
            else
           {
                   System.out.println(run2.getRunner() + " was in second with a time of " + run2.getTime());
                   System.out.println(run1.getRunner() + " was in last with a time of " + run1.getTime());
           }
       }
    }
}
<Thoatt> I was very much into fluid dynamics
(SrMod: ~Rowan) but your opinion on that was... fluid?
<Thoatt> up to ferrofluid in magnetic field dynamics
(SrMod: ~Rowan) that sounds like an attractive field
(SrMod: ~Rowan) I'm going hard on the puns right now XD

Offline eGI

  • Retired
  • Hero Member
  • *
  • Posts: 538
    • View Profile
Re: Coding Fun Part 2
« Reply #4 on April 10, 2014, 11:03:41 PM »
Woohoo... everything is fine now...  I even finally realized I had > instead of < and had things ordered from slowest to fastest instead of the other way around.
Head Coach Middle School Cross Country and Track
Assistant Coach High School Cross Country and Track

I know running... and a few other things...

Offline mathfreak2

  • Retired
  • Hero Member
  • *
  • Posts: 941
  • integral from 0 to 2sqrt(21) of x wrt x = 42
    • View Profile
Re: Coding Fun Part 2
« Reply #5 on April 11, 2014, 12:06:14 AM »
Perfect! I'm glad everything is making sense to you now! :D
<Thoatt> I was very much into fluid dynamics
(SrMod: ~Rowan) but your opinion on that was... fluid?
<Thoatt> up to ferrofluid in magnetic field dynamics
(SrMod: ~Rowan) that sounds like an attractive field
(SrMod: ~Rowan) I'm going hard on the puns right now XD

Offline 1869_Flame

  • Retired
  • Hero Member
  • *
  • Posts: 611
  • Home is where my dirt-hut is.
    • View Profile
Re: Coding Fun Part 2
« Reply #6 on April 11, 2014, 05:31:21 AM »
i suggest using C#, and i dont know java  :P