Space Pirates Ltd.
May 22, 2012, 11:07:19 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Yay Java  (Read 631 times)
Violent Unrest in Allentown
Administrator
Posting Entrepreneur

Posts: 4041



View Profile WWW Email
« on: October 17, 2006, 05:05:49 PM »

Nick knows what this is about.

Questions:
- In the quotient method, how do I get it to return the string "infinity" when the first number is 0?
- Do my loops statements look right?
-In the OperandsDriver class, how do I make sure the user only enters numbers, and if they don't enter numbers, how do I tell them to do so without crashing the app?
 - My tester class will also not communicate with my Operands class, making  alot of this untestable, so if you could advise why that is happening that would be great.


Code:
public class Operands {
private float operand1;
private double operand2;

public Operands(float operand1, double operand2) {
}

public double product() {
double x = 0;
double y = 0;
while (x < operand2) {
y += operand1;
x++;
}
return y;
}

public double quotient() {
double x = 0;
double y = 0;
(String)infinity = "Infinity";
if (operand1 == 0) {
return (String)infinity;
} else if (operand2 > operand1) {
x = 0;
return x;
}
do {
y = operand1 - operand2;
operand1 = (float) y;
x++;
} while (y > operand2);
return x;
}

public double power() {
double y = 0;
for (int i = 1; i < operand2; i++) {
y *= operand1;
}
return y;
}
}

Code:
import javax.swing.JOptionPane;

public class OperandsDriver {
public static void main(String[] args) {

String input1 = JOptionPane
.showInputDialog("Please enter the first integer:");
String input2 = JOptionPane
.showInputDialog("Please enter the second integer:");

float operand1 = Float.parseFloat(input1);
double operand2 = Double.parseDouble(input2);

String input3 = JOptionPane
.showInputDialog("Please enter 'p' for product, 'q' for quotient, 'pow' for power or 'y' to quit.");

while (input3 == "q") {
if (input3 == "p") {
Operands p = new Operands(operand1, operand2);
System.out.println("The product of " + operand1 + " and "
+ operand2 + " is " + p.product());
}

else if (input3 == "q") {
Operands q = new Operands(operand1, operand2);
System.out.println("The quotient of " + operand1 + " and "
+ operand2 + " is " + q.quotient());
}

else if (input3 == "pow") {
Operands pow = new Operands(operand1, operand2);
System.out.println("The power of " + operand1 + " and "
+ operand2 + " is " + pow.power());
}

else
{
System.out.println("Bad input.");
return;
}
}

}
}
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.8 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!