Mako
09-26-2002, 08:25 PM
Ok, heres the exact problem.
"Write a program that reads in the numerators and denominators of two fractions. The program should print the product of the two fractions as a fraction and a percent."
Edit: Fixed it :) turns out doubles dont like accepting integer input directly.
working code follows...
import javax.swing.JOptionPane;
public class ass2 {
public static void main(String[] args) {
// Prompt for user input of two fractions
String num1Str =
JOptionPane.showInputDialog("Enter the numerator of the first fraction");
int num1 = Integer.parseInt(num1Str);
String num2Str =
JOptionPane.showInputDialog("Enter the denominator of the first fraction");
int num2 = Integer.parseInt(num2Str);
String num3Str =
JOptionPane.showInputDialog("Enter the numerator of the second fraction");
int num3 = Integer.parseInt(num3Str);
String num4Str =
JOptionPane.showInputDialog("Enter the denominator of the second fraction");
int num4 = Integer.parseInt(num4Str);
// Multiply the numerators and denominators for the fraction
int numer=(num1 * num3);
int denom=(num2 * num4);
// Multiply the numerators and denominators for the percentage
double percnumer=(num1 * num3);
double percdenom=(num2 * num4);
// Converting to percentage
double perce=(percnumer/percdenom * 100);
// Output the fraction and percent
JOptionPane.showMessageDialog(null,
"The fraction is "+numer+"/"+denom+"." +
"\nIn Percentage form it is "+perce+"%.");
}
}
"Write a program that reads in the numerators and denominators of two fractions. The program should print the product of the two fractions as a fraction and a percent."
Edit: Fixed it :) turns out doubles dont like accepting integer input directly.
working code follows...
import javax.swing.JOptionPane;
public class ass2 {
public static void main(String[] args) {
// Prompt for user input of two fractions
String num1Str =
JOptionPane.showInputDialog("Enter the numerator of the first fraction");
int num1 = Integer.parseInt(num1Str);
String num2Str =
JOptionPane.showInputDialog("Enter the denominator of the first fraction");
int num2 = Integer.parseInt(num2Str);
String num3Str =
JOptionPane.showInputDialog("Enter the numerator of the second fraction");
int num3 = Integer.parseInt(num3Str);
String num4Str =
JOptionPane.showInputDialog("Enter the denominator of the second fraction");
int num4 = Integer.parseInt(num4Str);
// Multiply the numerators and denominators for the fraction
int numer=(num1 * num3);
int denom=(num2 * num4);
// Multiply the numerators and denominators for the percentage
double percnumer=(num1 * num3);
double percdenom=(num2 * num4);
// Converting to percentage
double perce=(percnumer/percdenom * 100);
// Output the fraction and percent
JOptionPane.showMessageDialog(null,
"The fraction is "+numer+"/"+denom+"." +
"\nIn Percentage form it is "+perce+"%.");
}
}