GIDForums

Go Back   GIDForums > Computer Programming Forums > Java Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Mar-2008, 11:04
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Calculator Java Class


Hello all I am a newcomer to the Java programming scene and I need help desperately with writing this class. This is the prompt given:

You will write an integer Calculator class. This Calculator will have the add, subtract, multiply, divide, power, clear, and getTotal() methods. However, you will not be allowed to use the multiplication (*), division (/) or power method of the Math class to write these methods, but will instead write loops using the add and / or subtract methods for the multiply, divide, and power methods.

I know how to do this the quick way without the loops but the whole loop thing is throwing me off and I do not know where to begin. I have to have at least done most of it by tomorrow. Please help.
  #2  
Old 24-Mar-2008, 11:05
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


Sorry this was also added:
  • This calculator can handle both positive and negative values.
  • An error message will be generated if there is an attempt to divide by 0
  • Anything to the power of 0, is 1.
  • Anything to the power of a negative value is undefined (because it will usually be a double result), and should generate an appropriate message.
  #3  
Old 24-Mar-2008, 11:27
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help ASAP with Calculator Java Class PLZ !


Go ahead and post what you have so far. Here is how I would do some of it:

JAVA Code:
public class CalculatorInteger
{ private int CurrentValue;

  public CalculatorInteger()
  { CurrentValue = 0;
  }

  public CalculatorInteger( int InitialValue )
  { CurrentValue = InitialValue;
  }

  public void Multiply( int Value )
  { // CurrentValue = CurrentValue * Value;
    int Result;
    boolean IsNegative = false;
    if(Value < 0) 
    { IsNegative = true;
      Value = 0 - Value;
    }
    for(int i = 0; i < Value; i++)
    { Result = Result + CurrentValue;
    }
    
    if( IsNegative ) Result = 0 - Result;

   CurrentValue = Result;
  }


}
  #4  
Old 24-Mar-2008, 12:58
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


Thank you so much fakepoo! Right now I am studying for Linear Algebra midterm which I need to ace to get a B at least. I had something similar to what you posted so I'm sorry to say I don't have much different in my code compared to yours. For me loops are very confusing and I've tried but I'm afraid I don't have the time to take a crash course in it now. College leaves me with so little time. This is an extra credit assignment which will really help me out a lot so if maybe someone can contribute I would forever be appreciative and would try to give back in any way possible. Thank you guys so much.
  #5  
Old 24-Mar-2008, 13:23
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


fakepoo I am receiving an error from your code. It says int Result should be int Result = 0 because it is not "initialized". I changed it to intResult = 0. That should be fine right? Once again thank you and if other can help I would be greatly appreciated!
  #6  
Old 24-Mar-2008, 13:27
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help ASAP with Calculator Java Class PLZ !


Yes, it should be initialized to zero. I just happened to miss it.
  #7  
Old 24-Mar-2008, 16:15
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


Please help ! =/
  #8  
Old 25-Mar-2008, 07:23
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 395
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Need help ASAP with Calculator Java Class PLZ !


What do you have so far? You should implement the Divide() method very much the same as the Multiply() method. Post your code and ask specific questions.
  #9  
Old 25-Mar-2008, 09:08
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


JAVA Code:
import java.util.*;
public class Calculator {

	// All of your code goes here
	
	private int CurrentValue;
	
	public Calculator(){
		
		CurrentValue = 0;
		
	}
	
	public Calculator(int initialValue){
	
		CurrentValue = initialValue;
		
	}
	
    public void clear(){
		//Resets the running total to 0
    	
    	CurrentValue = 0;
	}
	
    public void add(int value){
    	//Adds value to running total
    	
    	for(int i =0; i<value; i++){
    		CurrentValue++;
    	}
    }
	public void subtract(int value){
		//Subtracts value from the running total
		
		for(int i =0; i<value; i++){
    		CurrentValue--;
    	}
	}
	
	public void multiply(int value){
		
		{	// Multiplies running total by value.
			// CurrentValue = CurrentValue * Value;
			
		    int Result = 0;
		    boolean IsNegative = !((value>0)&&(CurrentValue>0));
		    if(value < 0) 
		    { IsNegative = true;
		      value = 0 - value;
		    }
		    for(int i = 0; i < value; i++)
		    { Result = Result + CurrentValue;
		    }
		    
		    if(IsNegative) Result = 0 - Result;

		    CurrentValue = Result;
		}
	
	}
	public void divide(int value){
		//Divides running total by value, generating an integer result. Checks if value is 0, if so, prints error
		boolean isNegative = !((value>0)&&(CurrentValue>0));
		boolean bothNegative = ((value<0)&&(CurrentValue<0));
		if(value == 0){
			System.out.println("Error!");
			return;
		}
		if(Math.abs(value)>Math.abs(CurrentValue)){
			CurrentValue = 0;
		}
		else{
			if(isNegative){
				if(value<0){
					value = 0 - value;
				}
				if(CurrentValue<0){
					CurrentValue = 0 -CurrentValue;
				}
			}
			int quotient = 0;
			while(CurrentValue>=value){
				CurrentValue-= value;
				quotient++;
			}
			CurrentValue = quotient;
		}
		if(isNegative&&!bothNegative){
			CurrentValue = 0 - CurrentValue;
		}
	}

	public void power(int pow){

	    //Raises running total to the power of pow. If pow is 0, running total is set to 1.
		boolean isNegative = CurrentValue < 0;
		if(pow==0){
			CurrentValue = 1;
		}
		else if(pow ==1){
			CurrentValue = CurrentValue;
		}
		else if(pow<0){
			System.out.println("Error!");
		}
		else{
			if(isNegative){
				CurrentValue = 0 - CurrentValue;
			}
			int index = CurrentValue;
			for(int i =1; i<pow; i++){
				this.multiply(index);
			}
		}
		if((pow%2==1)&&(isNegative)){
			CurrentValue = 0 - CurrentValue;
		}
	}
	
	public int getTotal(){
		//Returns current value of the running total
		
		return CurrentValue;
    
	}
	
		
}
Last edited by admin : 25-Mar-2008 at 21:41. Reason: Please insert your example Java codes between [JAVA] and [/JAVA] tags
  #10  
Old 25-Mar-2008, 09:09
sneakerhead724 sneakerhead724 is offline
New Member
 
Join Date: Mar 2008
Posts: 17
sneakerhead724 is an unknown quantity at this point

Re: Need help ASAP with Calculator Java Class PLZ !


thats what i have after no sleep and working on it hard...is that good? i think im done
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a Struct to a separate class usmsci CPP / C++ Forum 9 07-Feb-2007 08:34
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG CPP / C++ Forum 7 13-Nov-2006 15:11
C++ class -- Please help vnca_1 CPP / C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.