5 Replies - 9970 Views - Last Post: 12 May 2009 - 09:03 PMRate Topic:

Single Class with Constructor, methods, and Main Method...

Hey guys, I have a final project I've been working on, and I've figured out all the logic, however I'm having trouble as to actually allowing my program to use the methods i've written...

I'm unsure if I've encapsulated properly, and also, How can i use my methods and constructors in the main?? I get errors as I can't figure out how to create a new object using the main method, nor am I able to access the methods I've written.. Netbeans tells me i can't access non-static methods from a static context...so I'm at a loss of how to properly set up the permissions.

Here are the Project "requirements"

This project needs to be created as ONE Class file that will have the main method. When you create a new project in Netbeans you will come to a window shown below. Set the name of the main class as mentioned in the project description!

Create a MAIN class Ledger (The class should be encapsulated) that will record the sales for a store. It will have the attributes
• sale—an array of double values that are the amounts of all sales
• salesMade—the number of sales so far
• maxSales—the maximum number of sales that can be recorded

Following methods should be added to the class

• Ledger(max)—a constructor that sets the maximum number of sales to max
• addSale(d)—adds a sale, to the array, whose value is d
• getNumberOfSales—returns the number of sales made
• getTotalSales—returns the total value of the sales
• getAverageSale()—returns the average value of all the sales
• getCountAbove(v)—returns the number of sales that exceeded v in value
• A main method that will test this class. The main methods should be included in this class and not as a separate file. The main method should ask the user to enter a particular sale (call the methods to enter the sale after performing important illegal checks and checking if the array has an empty space) and then ask the user if the sales have finished through a loop. After the sales have been finished, show the following outputs: number of sales made, total sales, average sales and number of sales greater than a user entered sale price!

Rules:
1) Make sure that your program checks for illegal entries.
2) The program should be commented appropriately for easy understanding.
3) You class name should be “Ledger”. After you are finished with your programs (building and running it), please rename the file using the following format:

The class file should be named:
YourFullName_Project_5_Ledger.java

An example of the file name, if I were to write the program, would be RohitDua_Project_5_Ledger.java

how do i make a single java file: ok is is Dog.java with a main method: your public static void main() is therea constructor: : Dog(String name) is your constructoradditional methods: bark(int n) is a method. I wrote just one... you can add the Poo method and a way to call these from the main method Main is static it cannot call bark() which belong to a Dog object you have to pass through a Dog to access bark();. So I wroter a static method printDots() that main which is also static cab call

Re: Single Class with Constructor, methods, and Main Method...

Posted 12 May 2009 - 09:03 PM

All you need to do is create a Ledger object, and use that object to call your methods.

So, in the main method do this:

int n = WHATEVER_+NUMBER_YOU_NEED;
Ledger ledgerObject = new Ledger(n);
// Then, to call the methods, you would just do:
ledgerObject.setSalesMade(WHAT_AMOUNT_YOU_WANT);
// Repeat for whatever other methods you need to call.

Of course, just replace the all caps with whatever number you need to use in that location.

The only thing you may need to change is to make your Ledger(int x) constructor public.