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 4 of 4

Thread: How do I create an object(function)?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I create an object(function)?

    I am new to java, but have taken one VB class.
    I am working on a common exercise:
    Bags of coffee sell for $5.50.
    User orders a number of bags.
    number of bags * cost per bag.
    Bags are shipped in 3 sizes of boxes: large, medium, and small.
    Cost: Large= $ 1.80, Medium cost $1.00, and Small cost $0.60.
    The goal is to minimize cost- pack the bags most efficiently and cheaply.
    I have worked up to the equation: number of bags * cost per bag.
    I don't know how to create a function in java to do the other computing.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I create an object(function)?

    If you don't know this, I suggest you start at the beginning:
    Java Tutorials
    In particular:
    Defining Methods

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I create an object(function)?

    I guess I was not clear:

    Class object;
    object = new Class;

    What I meant was I wasn't clear how to word the function that computed
    the amount of boxes needed.

    Thank you for posting the links. They do explain what I need to do.
    Last edited by amarettoCoffee; January 18th, 2011 at 07:05 PM. Reason: Thank moderator and clarify.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do I create an object(function)?

    I suggest looking at how to define classes.

    public class Box
    {
        // your class definition goes here
     
        // this is a field
        private int fitsHowManyCoffees;
     
        // this is a constructor
        public Box(int fitsHowManyCoffees)
        {
            this.fitsHowManyCoffees = fitsHowManyCoffees;
        }
     
        // this is a method
        public int getFitsHowMany()
        {
            return fitsHowManyCoffees;
        }
    }

    public class User
    {
        public static void main(String[] args)
        {
            // this creates a new box which can hold 3 coffees
            Box holds3 = new Box(3);
            System.out.println("the box holds " + holds3.getFitsHowMany() + " coffees.");
        }
    }
    Last edited by helloworld922; January 18th, 2011 at 07:58 PM.

Similar Threads

  1. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  2. How to create a new object of another class within a method
    By davie in forum Object Oriented Programming
    Replies: 1
    Last Post: April 16th, 2010, 05:53 PM
  3. Create a CLOB object with the string value
    By oshoarun in forum JDBC & Databases
    Replies: 0
    Last Post: March 6th, 2010, 02:54 AM
  4. Replies: 2
    Last Post: February 25th, 2010, 04:17 PM
  5. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM