Advertisements

Monthly Archives: February 2014

Write a program SumOfTwoDice.java that prints the sum of two random integers between 1 and 6 (such as you might get when rolling dice).

//2-18-14
//TEST ** (double star) program
import java.lang.Math;

public class DicerollTEST{
public static void main(String[] args) {
int SIDES = 6;                                                                                //declares there 6 sides on a die and int SIDES
int a = 1 + (int) (Math.random() * SIDES);                       //Gives the random roll to one die
int b = 1 + (int) (Math.random() * SIDES);                      //Gives the random roll to the second die
int sum = a + b;                                                                            //Adds the sum of the two dice
System.out.println(sum);                                                       //Prints the sum of the 2 dice
}
}

Advertisements

Write a program that takes two int values a and b from the command line and prints a random integer between a and b.

import java.lang.Math;
import java.util.Scanner;

public class RandomNumbers{

public static void main(String args[])
{
System.out.println(“Enter a number “); //This prompts the user to enter a number
Scanner scan = new Scanner(System.in);
double t = scan.nextDouble(); //Stores the valuse as a double

System.out.println(“Enter a second number “); //This prompts the user to enter a 2nd number
double v = scan.nextDouble(); //Stores the valuse as a double

double w = Math.floor(Math.random()*(t-v+1)+v); //assigns w to a random number in between t & v
System.out.println(w); //prints w

}
}

Wind chill

Given the temperature t (in Fahrenheit) and the wind speed v (in miles per hour), the National Weather Service defines the effective temperature (the wind chill) to be:

w = 35.74 + 0.6215 t + (0.4275 t – 35.75) v0.16

Write a program WindChill.java that takes two double command-line arguments t and v and prints out the wind chill. Use Math.pow(a, b) to compute ab. Note: the formula is not valid if t is larger than 50 in absolute value or if v is larger than 120 or less than 3 (you may assume that the values you get are in that range).

 

Write a program SpringSeason.java that takes two int values

import java.util.Scanner;

public class SpringSeason{

public static void main(String args[])
{
System.out.println(“Enter the month “); //This prompts the user to enter a month
Scanner scan = new Scanner(System.in);
int month = scan.nextInt(); //Stores the valuse as an int

System.out.println(“Enter the day “); //This prompts the user to enter day
int day = scan.nextInt(); //Stores the valuse as an int

boolean wootwoot = (month == 3 && day >= 20 && day <= 31)
|| (month == 4 && day >= 1 && day <= 30)
|| (month == 5 && day >= 1 && day <= 31)
|| (month == 6 && day >= 1 && day <= 20);

System.out.println(wootwoot);

}
}

%d bloggers like this: