11 Replies - 541 Views - Last Post: 21 August 2010 - 04:33 PM Rate Topic: -----

#1 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

help with a simple class

Posted 19 August 2010 - 02:14 PM

hi,

im trying to learn java from where i left off, somewhere around classes and class design and all that. im trying to learn without relying solely on a book, as i know most of it, but i wont actually learn how to use it without a book unless im writing it independently. i couldnt think of anything better to attempt, so im trying to make a calculator class. it compiles so i know that it will technically work, but im looking more for advice on how things are supposed to be and if i am doing it correct. thanks for any help.

also, i had alot of trouble with how to set up the scanner. i have never technically figured out how to use the scanner in a method. i tried to have one method to initialize the scanner, another method to gain the first input, and another to gain the second input, but for some reason this wouldnt work. i just scratched that whole deal till i could find a way to do it correct.

from what i understand, the main method is just supposed to be method calls. im having a little trouble putting everything into a method, so some advice there would also be appreciated

/**
 *
 * @author Nathan
 */
import java.util.Scanner;
public class Calculator
{

    //initializer class variables
    private double a;
    private double b;
    private double total;

    //constructor
    public Calculator(double firstNumber, double secondNumber)
    {
        a = firstNumber;
        b = secondNumber;
    }

    //

    //gets the value of a
    public double getA()
    {
        return a;
    }

    //gets the value of b
    public double getB()
    {
        return b;
    }

    //gets the value of total
    public double getTotal()
    {
        return total;
    }


    //method to add the two inputs
    public double add()
    {
        total = a+b;
        return total;
    }

    //method to subtract the two inputs
    public double sub()
    {
        total = a-b;
        return total;
    }

    //method to multiply the two inputs
    public double mult()
    {
        total = a*b;
        return total;
    }

    //method to divide the two inputs
    public double div()
    {
        total = a/b;
        return total;
    }

    //i know that a toString method is supposed to output a 
    //string that represents the object, but im not really sure
    //what to put to give a good depiction
    public String toString()
    {
        return a + ", " + b;
    }
    

}



Is This A Good Question/Topic? 0
  • +

Replies To: help with a simple class

#2 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: help with a simple class

Posted 19 August 2010 - 02:26 PM

sorry, i forgot to add arguments to the methods.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5723
  • View blog
  • Posts: 22,635
  • Joined: 23-August 08

Re: help with a simple class

Posted 19 August 2010 - 02:33 PM

Why do you need a total member variable?
Was This Post Helpful? 1
  • +
  • -

#4 gibson.nathan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 294
  • Joined: 06-October 09

Re: help with a simple class

Posted 19 August 2010 - 02:38 PM

like i said, im not 100% on any of this. i just did it how i thought it was supposed to be done, and am trying to figure out what is wrong with it so i can alter how i think about it. but yea, since you mention it.. probably not necessary.
Was This Post Helpful? 0
  • +
  • -

#5 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: help with a simple class

Posted 19 August 2010 - 05:39 PM

when i first started java we were taught how to use methods.

try to sing happy birthday using methods.

public class Happy_Birthday
{
	public static void main(String[]args)
	{
		public void first_line()
		{
			System.out.println("Happy Birthday To You!");
		}
		
		public void second_line()
		{
			System.out.println("Happy Birthday Dear Bob!");
		}
		
		first_line();
		first_line();
		second_line();
		first_line();
	}
}


This will display

Happy Birthday To You!
Happy Birthday To You!
Happy Birthday Dear Bob!
Happy Birthday To You!


to go one step further you can add an input to your method.

public class Happy_Birthday
{
	public static void main(String[]args)
	{
		public void first_line()
		{
			System.out.println("Happy Birthday To You!);
		}
		
		public void second_line(String Name)
		{
			System.out.println("Happy Birthday Dear " + Name + "!");
		}
		
		first_line();
		first_line();
		second_line("Giuseppe");
		first_line();
	}
}


this one will output

Happy Birthday To You!
Happy Birthday To You!
Happy Birthday Dear Giuseppe!
Happy Birthday To You!


hope you understand more
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9155
  • View blog
  • Posts: 33,963
  • Joined: 27-December 08

Re: help with a simple class

Posted 19 August 2010 - 06:57 PM

To instantiate a Scanner to read from the console, just do such:
Scanner scan = new Scanner(System.in);



As System.in is an InputStream, that is the constructor invoked. Now for using it in a method, there are a bunch of ways to do such. You could have a getInput() method to handle everything (encapsulate Scanner in getInput()), or you could invoke getInput() defining it to accept a Scanner as a param. I generally do the latter for File I/O. Not really good practice to use a method per token of input. No need to have getFirstName(), getLastName(), getAge(), etc. input methods.

For the main() method, it should serve two purposes. First, in each class you have a main() method in, it should be for unit testing purposes only to insure the class and its methods work as intended. Instantiate an instance of the class and just play around with it. Second, for your Main class, your main() method should only kick start the program. The logic flow, etc. should be set up in another class, probably in the constructor.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2706
  • View blog
  • Posts: 10,579
  • Joined: 15-July 08

Re: help with a simple class

Posted 19 August 2010 - 08:09 PM

View Postgiuseppe105, on 19 August 2010 - 07:39 PM, said:

when i first started java we were taught how to use methods.

try to sing happy birthday using methods.

public class Happy_Birthday
{
	public static void main(String[]args)
	{
		public void first_line()
		{
			System.out.println("Happy Birthday To You!");
		}
		
		public void second_line()
		{
			System.out.println("Happy Birthday Dear Bob!");
		}
		
		first_line();
		first_line();
		second_line();
		first_line();
	}
}


This will display

Happy Birthday To You!
Happy Birthday To You!
Happy Birthday Dear Bob!
Happy Birthday To You!


to go one step further you can add an input to your method.

public class Happy_Birthday
{
	public static void main(String[]args)
	{
		public void first_line()
		{
			System.out.println("Happy Birthday To You!);
		}
		
		public void second_line(String Name)
		{
			System.out.println("Happy Birthday Dear " + Name + "!");
		}
		
		first_line();
		first_line();
		second_line("Giuseppe");
		first_line();
	}
}


this one will output

Happy Birthday To You!
Happy Birthday To You!
Happy Birthday Dear Giuseppe!
Happy Birthday To You!


hope you understand more



This is WAY wrong. This program doesn't even compile. You absolutely cannot have a method inside of a method. You do... Fix it like so:

public class Happy_Birthday
{
	public static void main(String[]args)
	{
		
		first_line();
		first_line();
		second_line();
		first_line();
	}

        public static void first_line()
	{
		System.out.println("Happy Birthday To You!");
	}
	
	public static void second_line()
	{
		System.out.println("Happy Birthday Dear Bob!");
	}
}


This post has been edited by Dogstopper: 20 August 2010 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 495
  • View blog
  • Posts: 1,451
  • Joined: 19-October 09

Re: help with a simple class

Posted 20 August 2010 - 01:51 AM

Does yours compile Dogstopper? :P
Was This Post Helpful? 1
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2706
  • View blog
  • Posts: 10,579
  • Joined: 15-July 08

Re: help with a simple class

Posted 20 August 2010 - 02:24 PM

View Postm-e-g-a-z, on 20 August 2010 - 03:51 AM, said:

Does yours compile Dogstopper? :P


*facepalm*

Now it does...thanks for the catch...
Was This Post Helpful? 0
  • +
  • -

#10 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 438
  • Joined: 15-May 08

Re: help with a simple class

Posted 20 August 2010 - 04:07 PM

my bad yea methods don't go in methods...

tnx for pointing that out i didn't compile the code
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: help with a simple class

Posted 20 August 2010 - 09:15 PM

Sorry

This post has been edited by pbl: 20 August 2010 - 09:18 PM
Reason for edit:: My bad, I should have scrolled down befor replying. Dogstopper had already replied to that

Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2706
  • View blog
  • Posts: 10,579
  • Joined: 15-July 08

Re: help with a simple class

Posted 21 August 2010 - 04:33 PM

View Postpbl, on 20 August 2010 - 11:15 PM, said:

Sorry


No problem pbl, we all do it accidentally (especially when multiple pages are involved...)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1