PDA

View Full Version : Newbie java question


Mako
09-26-2002, 07: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+"%.");


}
}

Strike
09-26-2002, 07:28 PM
// Converting to percentage
int perce=(numer/denom * 100);

You sure you want an integer there for a percentage? Not to mention that if you divide an integer by an integer, you will get an integer. Java 'floor's the result. So, anything between 1 and 0 will become 0 as an integer.

Mako
09-26-2002, 07:30 PM
I thought of this, tried it as a double, but the same result occurs.

Is it due to the inputs being integers? Because i was under the impression that java automatically changed integers into doubles when necessecary

Halide
09-26-2002, 07:35 PM
wassup are you the mako i think you are

Mako
09-26-2002, 07:37 PM
Originally posted by Halide
wassup are you the mako i think you are

of course :)

Halide
09-26-2002, 07:39 PM
hey java looks more fun than qbasic
that's what i'm learning here at CPCC :sick:

Halide
09-26-2002, 07:39 PM
just a note: strike said he was taking a shower and leaving for his girlfriend's in irc :)

Mako
09-26-2002, 07:42 PM
Originally posted by Halide
hey java looks more fun than qbasic
that's what i'm learning here at CPCC :sick:

Yeah java isnt all that bad. :)

Halide
09-26-2002, 07:43 PM
php > java

oh wait, wth do i know about "java" :eh:

Halide
09-26-2002, 07:45 PM
[edit] I can't even get this logic correct :) here goes try 2


if(php->leetness() > java->leetness())
{
java->delete();
echo "java has been owned";
}

gufmn
09-26-2002, 07:54 PM
This works:

double perce=((double)numer/(double)denom * 100);


You may want to look in to NumberFormat to limit number of digits in the percentage. So you dont end up with 54.325684125% or something.

Mako
09-26-2002, 07:56 PM
rofl, you so crazey

Java is a pretty powerful language, though undoubtedly held back by the virtual machine (although it provides tons of benifits).

It can even do OpenGL.

Mako
09-26-2002, 07:57 PM
Originally posted by gufmn
This works:

double perce=((double)numer/(double)denom * 100);


You may want to look in to NumberFormat to limit number of digits in the percentage. So you dont end up with 54.325684125% or something.

ahh ok thanks :) thats quite a bit smoother than what i used

nex
09-26-2002, 08:03 PM
Originally posted by Halide
hey java looks more fun than qbasic
that's what i'm learning here at CPCC :sick:

qbasic...

I've died and gone to 1987.

Shit.

Halide
09-26-2002, 08:12 PM
Originally posted by nex
qbasic...

I've died and gone to 1987.

Shit.

wouldn't that be cool... ?

Halide
09-26-2002, 08:14 PM
actually, I'm in qbasic class right now

check out what i am doing:

' File : EXER10.BAS
' About : Exercise 10 - Rectangle Program w/ conversational read loop
' Author : Joel Walters
' Date : September 26, 2002
' Class : CIS 115 - Section 11

CLS

INPUT "Are you ready to enter the first rectangle dimension (y/n)? ", moredata$

DO WHILE LCASE$(LEFT$(moredata$, 1)) = "y"
CLS

GOSUB get.b.and.h
GOSUB perimeter
GOSUB area
GOSUB header
GOSUB printresults

PRINT
INPUT "Are there more to be processed (y/n)? ", moredata$

LOOP

CLS

PRINT "Processing completed."

END

header:
CLS
PRINT "Rectangle Dimensions"
RETURN

get.b.and.h:
INPUT "Enter the base : ", b!
INPUT "Enter the height : ", h!
RETURN

perimeter:
p! = 2 * b! + 2 * h!
RETURN

area:
a! = b! * h!
RETURN

printresults:
p! = INT(p! * 100 + .5) / 100
a! = INT(a! * 100 + .5) / 100
PRINT
PRINT "Base :"; b!
PRINT "Height :"; h!
PRINT "Perimeter :"; p!
PRINT "Area :"; a!
RETURN

uber leet, right? :P

nex
09-26-2002, 08:27 PM
A qbasic class? :wtf:
where at?

Halide
09-26-2002, 08:30 PM
CPCC - Central Piedmont Community College

(Charlotte, NC, USA)

and why does everyone ask why there's a qbasic class? (it's simply an introduction to programming - you also learn flowcharting in this class)

(btw, an hour left to go) :(

on hour 3 of 4

nex
09-26-2002, 08:47 PM
I'm pretty sure most places teach C++ or Java as an introductory course now, and flowcharts have been outlawed for several years.

Next thing you know they'll be cramming COBOL down your throat.

Halide
09-26-2002, 08:53 PM
I'm sorry, but I can't help to laugh

I have COBOL M-F 10:30-11:20 am

:D