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.
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
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.
Re: How do I create an object(function)?
I suggest looking at how to define classes.
Code Java:
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;
}
}
Code Java:
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.");
}
}