5 Replies - 8201 Views - Last Post: 12 May 2009 - 09:03 PM Rate Topic: -----

#1 greatestone4eva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-May 09

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

Posted 10 May 2009 - 11:56 AM

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


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package project5.Main;
import javax.swing.*;

/**
 *
 * @author Azeem
 */
public class Ledger
{
	private int maxSales;
	private int salesMade;
	private double[] sales;
	Ledger(int x)
	{
		maxSales = x;
		sales = new double[maxSales];
	}//constructor sets the max sales to the max value as defined by user.

	double[] addSale(double d)
	{
		double[] ans = new double[getSales().length+1];
		System.arraycopy(getSales(), 0, ans, 0, getSales().length);
		ans[ans.length - 1] = d;
		return ans;
	}//copies existing sales and adds new element

	public boolean trufal(char yesno)
	{
		boolean yes = true;
		if(yesno == 'y'||yesno=='Y'){yes = true;}
		if(yesno == 'n'||yesno=='N'){yes = false;}
		return yes;
	}//currently this method is not used
		/**
	 * @return the maxSales
	 */
	public int getMaxSales() {
		return maxSales;
	}

	/**
	 * @param maxSales the maxSales to set
	 */
	public void setMaxSales(int maxSales) {
		this.maxSales = maxSales;
	}

	/**
	 * @return the salesMade
	 */
	public int getSalesMade() {
		return salesMade;
	}

	/**
	 * @param salesMade the salesMade to set
	 */
	public void setSalesMade(int salesMade) {
		this.salesMade = salesMade;
	}

	/**
	 * @return the sales
	 */
	public double[] getSales() {
		return sales;
	}

	/**
	 * @param sales the sales to set
	 */
	public void setSales(double[] sales) {
		this.sales = sales;
	}

	/**
	 * Program asks if user wants to enter sales.
	 * if yes, continuously loop until the user sets sales as finished, i.e. do while continue = true.
	 * program loops through the sales to be recorded
	 * during loop, user enters double value for array object for each sale which is error checked.
	 * method addsale returns new array with old values plus new value
	 * for each new sale input, copy src = previous new array from index 0 to end, and place in new array of +1length at index 0.
	 * 
	 * during loop, new array at last index = user input double
	 * when loop ends, the array is saved.
	 *
	 * getnumberofSales returns the amount of sales made (# times looped through)
	 * gettotal sales returns the sum of all the values in the final array.
	 * getAverageSale returns the mean of all the values in the finaly array.
	 * getCountabove returns all values in final array that are greater than userinput V
	 * 
	 */
	public static void main(String[] args) 
	{
		int Continue = JOptionPane.showConfirmDialog(null, "Would you like to begin entering sales?", "Welcome to Ledger!", JOptionPane.YES_NO_OPTION);

		do
		{
			double input = Double.parseDouble(JOptionPane.showInputDialog("Please enter the sales amount:"));
			Ledger.setMaxSales();
			
			Continue = JOptionPane.showConfirmDialog(null, "Would you like to continue entering sales?", "Continue?", JOptionPane.YES_NO_OPTION);
			
			//System.out.println("Continue = " + Continue);
		}while(Continue == JOptionPane.YES_OPTION);

		// TODO code application logic here
	}

}


This post has been edited by greatestone4eva: 10 May 2009 - 12:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Single Class with Constructor, methods, and Main Method...

#2 phil1000  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 176
  • Joined: 26-February 09

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

Posted 10 May 2009 - 06:13 PM

In java, something non-static can't be called within a static class. main() is static class and you're trying to call Ledger that isn't static. If you change the first line to

public static class Ledger


I believe that should work. I haven't really programmed like this, but that should fix your problem.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

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

Posted 10 May 2009 - 07:02 PM

Kind of confusing

int Continue = JOptionPane.showConfirmDialog(null, "Would you like to begin entering sales?", "Welcome to Ledger!", JOptionPane.YES_NO_OPTION);

continue is one of the few Java reserved keyword
not a good idea at all to create an int variable named Continue
you need to be kind of paranoiac or to tease the devil to do this kind of thing
Was This Post Helpful? 0
  • +
  • -

#4 greatestone4eva  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-May 09

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

Posted 12 May 2009 - 05:02 PM

Unfortunately, netbeans tells me that the static modifier is not allowed for my class declaration.

Also, thanks for letting me know that Continue was one of the reserved words...didn't know that.

so to clarify a bit, how do i make a single java file with a main method, a constructor, additional methods, and a way to call these from the main method?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

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

Posted 12 May 2009 - 07:56 PM

View Postgreatestone4eva, on 12 May, 2009 - 04:02 PM, said:

so to clarify a bit, how do i make a single java file with a main method, a constructor, additional methods, and a way to call these from the main method?


Theorically you can't called instance method from a main static method


class Dog {
	  
	 String dogName;

	 // construtor
	 Dog(Sring name) {
		 dogName = name;
	 }

	 // makes a dog bark nbTimes
	 void bark(int nbTimes) {
		 System.out.print(dogName + " says: ");
		 for(int i = 0; i < nbTimes; i++)
			 System.out.print(" Bark!");
		 System.out.println();
	 }

	 // this is the main method to start the program
	 public static void main(String[] args) {
		// create 2 dogs
		Dog dog1 = new Dog("Fifo");
		Dog dog2 = new Dog("Brutus");
		// make them bark
		dog1.bark(10);
		printDots();
		dog2.bark(4);
		printDots();
	 }

	 static void printDots() {
		 System.out.println("--------------------------");
	  }
}



how do i make a single java file: ok is is Dog.java
with a main method: your public static void main() is there
a constructor: : Dog(String name) is your constructor
additional methods: bark(int n) is a method. I wrote just one... you can add the Poo method :D
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
Was This Post Helpful? 1
  • +
  • -

#6 llemons  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-May 09

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.

This post has been edited by llemons: 12 May 2009 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1