Log in

View Full Version : My java class is killing me!!!!


Ganga
2004-03-28, 06:21 PM
Anyone here knows java!!!!!!!....... I need help bad.. I am getting a C probally worst... at that class.

Neko
2004-03-28, 06:26 PM
My condolences, Java's a pretty confusing language to learn, at least from what I've looked at. At least you're learning (or trying to learn) something you can use. My computer class is fricking boring and what we're learning probably won't serve me any use in the future. Also my grade isn't so good in it for this quarter...

Demosthenes
2004-03-28, 06:39 PM
Actually Java's pretty easy. I'm not a Java guru or anything, but I could try to help you out if need be. What are you having trouble with Ganga?

Ganga
2004-03-29, 06:59 PM
it's not hard, but we just started to learn, and we are having this crazy ass homework, we have to write a program for a caculator is our current project... after I spend 10 hour in TA office I think I got it, when my new project come up...which will be right after this one due- tomorow, I ask you question.

Demosthenes
2004-03-29, 07:04 PM
No problem. Feel free to ask me whatever you feel like. Another thing, check out www.planetsourcecode.com. Whatever your homework is, I'm sure they'll have something similar to it there, so you can use that as reference if the need arises.

`Insolence`
2004-03-30, 01:06 AM
Wow, java can't be THAT hard to write a calc, ever used any other programming langs?

Ganga
2004-03-30, 08:21 AM
no it's suppose to be first programming class.

Ganga
2004-03-30, 08:28 AM
public class Calculator implements CalculatorMVC {
private CalculatorFrame frame;
private String display; //Contents which will be display
private boolean wasLastkeyOperator=true; //show was last key a operator
private int firstOperand; //first operation
private char preOp; //Previews operator

static public void main(String[] args) {
Calculator calc = new Calculator();
CalculatorFrame frame = new CalculatorFrame("Calc");
frame.setCalculatorMVC(calc);
}

public Calculator() {
display = "0"; //int display content
preOp = '='; //int previes operator
}

public String getDisplayString() {

return display; // return content
}

public void keyClicked(char key){
if (key == 'C'){
display = "0";
firstOperand = 0;
wasLastkeyOperator = false;
}

// If it's a operator stop, else continue
//Performs the indicated mathematical operation according to whether a sequence of

if (isNumber(key)) {
if (wasLastkeyOperator){
display = ""+key;
wasLastkeyOperator = false;
}else {
display +=key;
}
}else
if (wasLastkeyOperator){
preOp = key;
}
//Calculation
else {
if (preOp=='+'){
firstOperand =firstOperand + Integer.parseInt (display);
display = Integer.toString (firstOperand);
}
if (preOp =='-') {
firstOperand =firstOperand - Integer.parseInt (display);
display = Integer.toString(firstOperand);
}
if (preOp == '*') {
firstOperand =firstOperand * Integer.parseInt (display);
display = Integer.toString(firstOperand);
}
if (preOp == '/') {
if (Integer.parseInt(display)==0){
display = "Divide by zero";
}
else{
firstOperand =firstOperand / Integer.parseInt (display);
display = Integer.toString (firstOperand);
}
}
if (preOp == '=') {
firstOperand = Integer.parseInt(display);
}
preOp =key;
wasLastkeyOperator = true;
}
}
//see if the key is a number or a operator
public boolean isNumber(char key){
if (key >= '0' && key <='9'){
return true;
}
else {
return false;
}
}
}






That's my code for the simple calculator, but my problem is everytime I click clear (C) the after calculation always wrong, it will ingnore my first number I input then it will be 0+ my second number or 0- second number or 0 * or 0 /,
like 9+9 =9, 9-9 =-9, 9*9=0, 9/9=0 where all the first number input is 0.
so I am thinking there something wrong with my codes around here:

if (key == 'C'){
display = "0";
firstOperand = 0;
wasLastkeyOperator = false;
}








edit: Nvm I just need to add a else after it. got it, thx anyway MJ.