Commit dce0515d authored by ryanw's avatar ryanw

week five exercises

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>weekFiveExercises</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package weekFiveExercises;
import java.util.Random;
import java.util.Scanner;
public class Method_Exercises
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
//System.out.print("The perimeter of this triangle is: " + perim(5, 5, 5));
//System.out.println("\nThe area of this triangle is: " + area(5 ,5, 5));
//tempMenu();
//int n = 10;
//System.out.println(fibonacci(n));
int n = 2;
int k = 3;
//System.out.println(power(n, k));
guessGame();
}
public static int perim(int a, int b,int c)
{
int perim = a + b + c;
return perim;
}
public static double area(int a, int b,int c)
{
int s = (a + b + c)/2;
double area = Math.round(Math.sqrt(s * (s-a) * (s-b) * (s-c)));
return area;
}
public static void tempMenu()
{
//repeat menu until the user selects quit
while(true)
{
System.out.println("Please choose from the following:");
System.out.println();
System.out.println("1 - Celcius to Fahrenheit");
System.out.println("2 - Fahrenheit to Clecius");
System.out.println("3 - Quit");
String choice = input.next();
switch (choice)
{
case "1":
celToFah();
break;
case "2":
fahToCel();
break;
default:
System.out.println("Goodbye.");
System.exit(0);
break;
}
}//while loop ends
}
private static void celToFah()
{
System.out.println("Please enter the temp in Celcius: ");
int cel = input.nextInt();
double fah = 9.0 * cel/5 + 32;
System.out.println(cel + "in Fahrenheit is: " + fah);
}
private static void fahToCel()
{
System.out.println("Please enter the temp in Fahrenheit: ");
int fah = input.nextInt();
double cel = (fah - 32) * 5/9;
System.out.println(fah + "in Celcius is " + cel);
}
public static int sum(int x, int y)
{
int result = x + y;
return result;
}
public static int product(int a, int b, int c)
{
int result = a * b * c;
System.out.println("Result is: " + result);
return result;
}
public static int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
public static double power(double n, double k)
{
double power = Math.pow(n, k);
System.out.println(n + "to the power of " + k + "is: ");
return power;
}
public static void isMultiple()
{
}
public static void guessGame()
{
while(true)
{
Random rand = new Random();
int guessNum = rand.nextInt(1001);
//take guess from user
System.out.print("Please enter your guess from 1 - 1000: ");
int guess = input.nextInt();
if(guess > guessNum)
{
System.out.println("Too high, please try again");
}
else if(guess < guessNum)
{
System.out.println("Too low, please try again");
}
else if(guess == guessNum)
{
System.out.println("Correct, that was the right guess!");
break;
}
}//end of while loop
System.out.println("Do you want to play again? Y/N");
String choice = input.next();
if(choice == "y")
{
guessGame();
}
else
System.out.println("Goodbye!");
System.exit(0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment