Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Question about creating and using classes.

  1. #1
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Question about creating and using classes.

    Hi everyone. So I have edited this post with another assignment that I'm working on. I am writing a class called bandBooster that contains methods to update candy sales. So my question is how do I use the class that I created below to implement it into another program. When I try to write another program, the method names are not recognized in their. I'm using Eclipse SDK as the compiler and I know it creates a .java and a .class file. Is there something I need to do like open both files in order to use the class in another program or something?

    The class that I want to use:
    public class bandBooster
    {
    	private String name;
    	private int boxesSold;
     
    	public void boosterName(String name)
    	{
    	boxesSold=0;
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public int updateSales(int sold)
    	{
    		boxesSold=boxesSold+sold;
    		return boxesSold;
    	}
    	public String toString()
    	{
    		return name + ":" + boxesSold + "boxes";	
    	}
     
    }



    This is the program that I want to use the methods from the above class in:
    import java.util.Scanner;
    public class boosterSales
    {
    	public static void main(String[] args) 
    	{	
    		name name1=new name(Joe);
    		name name2=new name(Bob);
     
    		boxesSold sold1=new sold2;
    		boxesSold sold2=new sold2;
     
     
    		Scanner scan=new Scanner(System.in);
    		System.out.println("How many boxes did Joe sell this week?");
    		sold1=scan.nextInt();
     
    		name1.updateSales(boxesSold);
     
    		System.out.println("This week Joe sold; " + boxesSold);
     
    	}
     
    }
    Last edited by iDizzle; March 30th, 2012 at 07:05 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    So how would I use this class to write a separate program?
    To make a program from a class, add a main() method to it. The main() method will have to create an instance of the class and call its methods as needed.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    Quote Originally Posted by Norm View Post
    To make a program from a class, add a main() method to it. The main() method will have to create an instance of the class and call its methods as needed.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    Ah, thank you! That looks much better. So to add a main I would add it below all of that code above? What did you mean by the main() will create an instance of the class? Sorry, I'm a beginner beginner. =/

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    You create an instance of a class by using a new statement:
    new TheClassName();

    What do you want or expect the class to do when you execute it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    Hi guys. So I updated the first post with more of what I'm trying to do. For an assignment I need to create a class and write another program using methods from that class. When I try to use them method names they are unrecognized and don't work. Any info would be greatly appreciated. Thanks!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    try to use them method names they are unrecognized
    That sounds like a compiler error. Please copy and paste here the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    This is the error that I get.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    name cannot be resolved to a type
    name cannot be resolved to a type
    Duplicate local variable name1
    name cannot be resolved to a type
    Joe cannot be resolved to a variable
    name cannot be resolved to a type
    Duplicate local variable name2
    name cannot be resolved to a type
    Bob cannot be resolved to a variable
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    sold2 cannot be resolved to a variable
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    boxesSold cannot be resolved to a variable
    boxesSold cannot be resolved to a variable

    at boosterSales.main(boosterSales.java:17)

  8. #8
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    This is the error that I get.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    name cannot be resolved to a type
    name cannot be resolved to a type
    Duplicate local variable name1
    name cannot be resolved to a type
    Joe cannot be resolved to a variable
    name cannot be resolved to a type
    Duplicate local variable name2
    name cannot be resolved to a type
    Bob cannot be resolved to a variable
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    sold2 cannot be resolved to a variable
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    boxesSold cannot be resolved to a variable
    boxesSold cannot be resolved to a variable

    at boosterSales.main(boosterSales.java:17)

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    Those are not compiler error messages. They are generated by the IDE. Those messages are missing a lot of information about the errors. The IDE must give you some visual indication of the error in the source. I can't see that indication. Pick one message and try to change the program to fix it. For example where is name defined?

    Here is what a compiler error message looks like:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    Notice it has the source line number, the source line, a ^ under the location of the error and a message describing the error.

    Use the javac command to get useful error messages.
    Last edited by Norm; March 30th, 2012 at 07:10 PM. Reason: added c to java
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    My compiler has little "x"s at the beginning of the line to annotate errors and for example, line 6 says;
    ----------------------------------------------------------------------
    Joe cannot be resolved to a variable
    name cannot be resolved to a type
    name cannot be resolved to a type
    Duplicate local variable name1
    name1 cannot be resolved to a type
    ----------------------------------------------------------------------
    So to me it looks like the variables that were declared in the class are not being recognized when I try to use them

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    Sorry I can not see the 'x's from your IDE (not compiler)
    Use the javac command to get the compiler error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    Sorry, how do I use the java command? I'm a beginner =[

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    open a command prompt window, use the change directory (CD) command to change to the directory with the source file: (.java) and enter the command:
    javac <THESOURCEFILENAMEHERE>.java

    If the javac command is not found by the OS, it will give a message.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    So I guess my question is, in order to use the methods and variables from a class(bandBooster) that I've created, in the new program(boosterSales) do I have to open both files at the same time? Or do I need to import the bandBooster class into the boosterSales program some how? Or do I need to put the main of the boosterSales program directly below the bandBooster class? I think the compiler program that I'm using to do this is just seeing the bandBooster class and the boosterSales program as standalone programs.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    in order to use
    The javac command must be able to find definitions for the classes that you use in a java source file when it is compiling that source file. The definition files for the classes should be on the classpath for the javac command.
    If all the class definitions are in the same folder the javac command should be able to find them.
    If any of the classes are in packages then the classpath must point to the folder that holds the package folder.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    ohh ok. Well, both of the files are in the same folder. They are both in the "bin" folder and both in the "src" folder.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    Can you post the full text of the compiler's error messages?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    As far as I know, these are the error messages that it's giving me. These error messages are from the boosterSales program that I'm trying to use the bandBooster class methods and variables from. No errors in the bandBooster class. But only in the boosterSales program. Both the bandBooster class and the boosterSales program are in the same project.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    name cannot be resolved to a type
    name1 cannot be resolved to a type
    Joe cannot be resolved to a variable
    name cannot be resolved to a type
    name2 cannot be resolved to a type
    Bob cannot be resolved to a variable
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    boxesSold cannot be resolved to a type
    Syntax error on token "new", delete this token
    boxesSold cannot be resolved to a variable
    boxesSold cannot be resolved to a variable

    at boosterSales.main(boosterSales.java:17)

  19. #19
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    Sorry, this is all just so confusing to me. My text book is very vague about this. I understand the concept of creating and using a class but I'm just having trouble putting it into use. Thanks for your help, I appreciate it.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,422
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Question about creating and using classes.

    name cannot be resolved to a type
    A type is the name of a class or a primitive like int. By convention class names should start with uppercase letters.
    You are using the symbol: name where there should be the name of a class. For example String is the name of a class.

    You need to look at your text book to see how to define variables. Your syntax is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    iDizzle (April 1st, 2012)

  22. #21
    Member iDizzle's Avatar
    Join Date
    Mar 2012
    Location
    Los Angeles, CA
    Posts
    45
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question about creating and using classes.

    Will do, thank you! Yeah because I did not know where the problem was coming from. As long as I know its a syntax error than I know where to being. Thanks again! I'll see how it works out afterwards.

Similar Threads

  1. Replies: 4
    Last Post: October 24th, 2011, 08:09 AM
  2. Frames - Using and creating (+another question)
    By 12sea21 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 1st, 2011, 01:24 AM
  3. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM
  4. noob question on abstract classes
    By joshred in forum Object Oriented Programming
    Replies: 1
    Last Post: September 15th, 2010, 06:27 PM
  5. A Question Regarding Abstract Classes & Packages
    By Kerubu in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:27 PM