Skip to content

Instantly share code, notes, and snippets.

@mathur
Created November 9, 2012 17:39
Show Gist options
  • Save mathur/4047055 to your computer and use it in GitHub Desktop.
Save mathur/4047055 to your computer and use it in GitHub Desktop.
Java Math
/**
* Circle.java prints the area of a circle with two different radii
*
* @author Rohan Mathur
* @date 10/24/12
* @period 7
*/
import java.util.Scanner;
public class RMCircle
{
public static void main(String[] args)
{
//Initialize the variables
Scanner keyboard = new Scanner(System.in);
int radius; //1 + (int)(Math.random()*100); //Random Integer in the set (11,100]
System.out.print("Enter an integer value radius: ");
radius = keyboard.nextInt();
if (radius <= 0)
System.out.println("Radius is invalid. Must be greater than zero.");
else
{
double area1 = Math.PI * Math.pow(radius,2);
double circumference1 = 2 * Math.PI * radius;
double area2, circumference2, areaRatio, circumferenceRatio;
//Output the information for the first circle
System.out.printf("The area of a circle with radius %d is %.3f\n", radius, area1);
System.out.printf("The circumference of the circle is %.3f\n", circumference1);
//Assign the values for the
radius *= 2;
area2 = Math.PI * Math.pow(radius,2);
circumference2 = 2 * Math.PI * radius;
System.out.printf("The area of a circle with radius %d is %.3f\n", radius, area2);
System.out.printf("The circumference of the circle is %.3f\n", circumference2);
areaRatio = (area2 / area1);
System.out.printf("The area ratio between the two circles is %.3f\n", areaRatio);
circumferenceRatio = (circumference2 / circumference1);
System.out.printf("The circumference ratio between the two circles is %.3f\n", circumferenceRatio);
}
}
}
/**
* @(#)PhoneNumber.java
*
* Phone Number randomly a phone number using Math.random method and outputs
* in the form (XXX) XXX-XXXX where the area code and exchange code must not begin with a 0 or 1
* and then utilizes the printf method and DecimalFormat class to output the phone number
*
* @author Rohan Mathur
* @date 10/24/12
* @period 7
*/
import java.util.Random;
import java.text.DecimalFormat;
public class RMPhoneNumber
{
public static void main(String[] args)
{
//Utilize Math.random() to create a random area code, exchange code,
// and subscriber line number (the last four digits)
int areaCode, exchangeCode, lastFourDigits;
areaCode = 200 + (int) (Math.random() * 800);
exchangeCode = 200 + (int) (Math.random() * 800);
lastFourDigits = (int) (Math.random() * 10000);
//Output using printf method in the form (XXX) XXX - XXXX
//Hint: make sure my phone number (800) 555 - 0987 prints correctly.
System.out.printf("(%3d) %3d - %04d\n", areaCode, exchangeCode, lastFourDigits);
Random generator = new Random();
areaCode = generator.nextInt(800) + 200;
exchangeCode = generator.nextInt(800) + 200;
lastFourDigits = generator.nextInt(10000);
DecimalFormat fmt = new DecimalFormat("0000");
System.out.println("(" + areaCode + ") " + exchangeCode + " - " + fmt.format(lastFourDigits));
//System.out.printl("(%3d) %3d - %04d", areaCode, exchangeCode, lastFourDigits);
}
}
/**
* QuadraticFormula.java
*
* Computes and Solves Quadratic Equations in the form ax^2+bx+c=0
* Outputs two solutions given to four decimal places.
*
* @author Rohan Mathur
* @date 10/24/12
* @period 7
*/
import java.util.Scanner;
import java.text.DecimalFormat;
public class RMQuadraticFormula
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("#.####");
//Use Math.random() to assign random integers in the range [-25,25]
int a, b, c;
//a = (int) (Math.random() * 51) - 25;
//b = (int) (Math.random() * 51) - 25;
//c = (int) (Math.random() * 51) - 25;
System.out.println("Solve a Quadratic Equation with integer coefficients");
System.out.print("a = ");
a = keyboard.nextInt();
System.out.print("b = ");
b = keyboard.nextInt();
System.out.print("c = ");
c = keyboard.nextInt();
//Calculate the discriminant here
double discriminant;
discriminant = Math.pow(b,2) - (4 * a * c);
//Check if discriminant is positive or negative
if (discriminant <0 )
{
discriminant = Math.abs(discriminant);
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(discriminant)/(2 * a);
String answer1 = realPart + " + " + imaginaryPart + "i";
String answer2 = realPart + " - " + imaginaryPart + "i";
System.out.printf("The solutions to %dx^2+%dx+%d=0 are %s and %s\n", a, b, c, answer1, answer2);
}
else
{
//Compute the two answers here
double answer1 = ((-b + Math.sqrt(discriminant))/(2 * a));
double answer2 = ((-b - Math.sqrt(discriminant))/(2 * a));
//Output the results to four decimals in an organized format using printf
System.out.printf("The solutions to %dx^2+%dx+%d=0 are %.4f and %.4f\n", a, b, c, answer1, answer2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment