12 Replies - 515 Views - Last Post: 21 February 2011 - 06:34 PM Rate Topic: -----

#1 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Using constructors and method calls

Posted 20 February 2011 - 07:09 PM

I am working on a project for class that involves building a class constructor and calling up methods to display some kind of information. I want it to take input for the amount of land a farmer has and return how many gallons of water and how much seed he will need. I just want to see if I am on the right track with this and if I need a get() method somewhere and why I have invalid method declarations. All of your help is greatly appreciated.
import javax.swing.JOptionPane
public class Agricultural
{
    public static void main(String [] args);
{
    int water; //Water variable
    int seed; //seed variable
    int acreage;  //acreage variable
    Land landobject = new land (gallonsneeded, seedneeded, acreage);

    JOptionPane.showMessageDialog(
    null, "Welcome to Agricultural R Us, for all your agricultural consulting needs");
    
    seed= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many seeds will you be planting?"));

    water= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many gallons of water will you need?"));

    acreage= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many acres of land do you own?"));

	JOptionPane.showMessageDialog (
	null, "What you need is listed below"
	+ Land.computeLand( seedneeded, waterneeded, acreage) + " .");
       } 
}//main

class Land {
    private class int computeLand(int water, int seed, int acreage);
{
    int gallonsneeded;
    int seedneeded;
    int acreage;
    gallonsneeded= acreage * waterneeded;
    seedneeded= acreage * seedneeded;
	dailytotal= gallonsneeded, seedneeded;
    return dailytotal;
}
    
    Land(int water, int seeds, int acreage);
    {
        gallonsneeded = water;
        seedneeded = seed;
        acreage;
    }
    Land(int gallonsneeded);
    {
        this (1, gallonsneeded, 1);
    }
    Land(int seedneeded);
    {
        this (seedneeded, 1, 1);
    }
    Land(int acreage);
    {
        this (1, 1, acreage);
    }
    Land();
    {
        this (1, 1, 1);
    }//class

}



Is This A Good Question/Topic? 0
  • +

Replies To: Using constructors and method calls

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Using constructors and method calls

Posted 20 February 2011 - 07:14 PM

This line is wrong. A class CANNOT be a method. No need for the class keyword here at all. Also, the semi-colon at the end of this method defniition is illegal b/c the method isn't abstract.
private class int computeLand(int water, int seed, int acreage); 


Was This Post Helpful? 0
  • +
  • -

#3 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 20 February 2011 - 07:15 PM

Well thank you I am still learning, probably why it reached the end of the file without parsing too.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Using constructors and method calls

Posted 20 February 2011 - 07:17 PM

I just noticed you have semi-colons after all your method definitions. Go ahead and remove those too. In the future, please remember to post your exact compilation errors from your compiler. :)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Using constructors and method calls

Posted 20 February 2011 - 07:44 PM

a lot of extra ; in that code

; are to finish a code instruction or a variable declaration not a class definition
Was This Post Helpful? 0
  • +
  • -

#6 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 20 February 2011 - 09:08 PM

Oh this helps a lot, because the compiler was saying it was missing them in places I got a little out of hand. I will take this great tip to heart and probably should've known about it sooner. I will also remember to post my exact errors from the console. Should I just save the file to a text and copy paste or attach the file?

It is saying that acreage isn't a statement in my constructor? It also says my dailyTotal is missing a semi colon. How do I make it so it returns my seedneeded and gallons needed to the other class properly?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Using constructors and method calls

Posted 20 February 2011 - 09:45 PM

Post your revised code and exact error messages from your compiler.
Was This Post Helpful? 0
  • +
  • -

#8 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 20 February 2011 - 11:28 PM

My revised code and following that is the compiler error messages.
/*Agricultural R Us program created by Jose Coronel, Sue Anne Seeser, Ryan Jones */

import javax.swing.JOptionPane;
public class Agricultural
{
    public static void main(String [] args)
{
    int water; //Water variable
    int seed; //seed variable
    int acreage;  //acreage variable
    Land landobject = new land ();

    JOptionPane.showMessageDialog(
    null, "Welcome to Agricultural R Us, for all your agricultural consulting needs");
    
    seed= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many seeds will you be planting?"));

    water= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many gallons of water will you need?"));

    acreage= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many acres of land do you own?"));

	JOptionPane.showMessageDialog (
	null, "What you need is listed below"
	+ Land.computeLand( seedneeded, waterneeded, acreage) + " .");
       } 
}//main

class Land {
    private int computeLand(int water, int seed, int acreage)
{
    int gallonsneeded;
    int seedneeded;
    int acreage;
    gallonsneeded= acreage * waterneeded;
    seedneeded= acreage * seedneeded;
	dailytotal= (gallonsneeded, seedneeded);
    return dailytotal;
}
    
    Land(int water, int seeds, int acreage)
    {
        gallonsneeded = water;
        seedneeded = seed;
        acreage;
    }
    Land(int gallonsneeded);
    {
        this (1, gallonsneeded, 1);
    }
    Land(int seedneeded);
    {
        this (seedneeded, 1, 1);
    }
    Land(int acreage);
    {
        this (1, 1, acreage);
    }
    Land();
    {
        this (1, 1, 1);
    }//class

}




Microsoft Windows [Version 6.1.7600]
Copyright © 2009 Microsoft Corporation. All rights reserved.

C:\Users\Khoury>cd desktop\Java CIT-63

C:\Users\Khoury\Desktop\Java CIT-63>javac Agricultural.java
Agricultural.java:42: ')' expected
dailytotal= (gallonsneeded, seedneeded);
^
Agricultural.java:42: not a statement
dailytotal= (gallonsneeded, seedneeded);
^
Agricultural.java:42: ';' expected
dailytotal= (gallonsneeded, seedneeded);
^
Agricultural.java:50: not a statement
acreage;
^
4 errors

C:\Users\Khoury\Desktop\Java CIT-63>
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10667
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Using constructors and method calls

Posted 20 February 2011 - 11:36 PM

Where do you declare the dailytotal variable? I don't see it declared anywhere. Also note that your method computeLand() returns an int. This is singular. You cannot return two values in one method. Perhaps it would be better to design a class to encapsulate gallonsneeded and seedneeded, and return an instance of that class. Or return an int[] instead. Either way, you will need to change the return type of that method.

This is no more valid Java than standing up in the middle of a meeting and shouting "acreage!" is valid grammar or ettiquete. You aren't saying anything.
acreage;


Was This Post Helpful? 0
  • +
  • -

#10 MrHiD3f  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-February 11

Re: Using constructors and method calls

Posted 20 February 2011 - 11:40 PM

Quote

C:\Users\Khoury\Desktop\Java CIT-63>javac Agricultural.java
Agricultural.java:42: ')' expected
dailytotal= (gallonsneeded, seedneeded);


That is not a statement. To calculate your dailytotal create a return method that would take in the parameters of 'gallonsneeded' and your 'seedneeded'
Was This Post Helpful? 0
  • +
  • -

#11 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 20 February 2011 - 11:47 PM

oh ok so the daily total should be the return, it is coming together. Your joke about my acreage was pretty funny and I do see that I need to declare it an int now. I really appreciate all the help. I am mastering notepad so I actually learn the full language. No dev environment for me yet.
Was This Post Helpful? 1
  • +
  • -

#12 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 21 February 2011 - 06:17 PM

So I guess I am still lost, I should create another class named dailyTotal with the parameters of gallonsneeded and seedneeded to call it from the Land class? Then I will declare those same variables again with a return method for each? Then I will change my computeLand on line 30 to the dailyTotal class with the parameters of gallonsneeded and seedneeded? Once I work out my problems with calling methods life should be a lot easier.
Was This Post Helpful? 0
  • +
  • -

#13 mostdef  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 07-February 11

Re: Using constructors and method calls

Posted 21 February 2011 - 06:34 PM

So I made a seperate method inside the Land class the light may have come on, but I am not sure what to do here?

C:\Users\Khoury\Desktop\Java CIT-63>javac Agricultural.java
Agricultural.java:30: '.class' expected
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");
^
Agricultural.java:30: '.class' expected
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");
^
Agricultural.java:30: not a statement
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");
^
Agricultural.java:30: ';' expected
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");
^
Agricultural.java:30: not a statement
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");
^
Agricultural.java:30: ';' expected
+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .
");

^
6 errors



here is my revised code

import javax.swing.JOptionPane;
public class Agricultural
{
    public static void main(String [] args)
{
    int water; //Water variable
    int seed; //seed variable
    int acreage;  //acreage variable
    Land landobject = new land ();

    JOptionPane.showMessageDialog(
    null, "Welcome to Agricultural R Us, for all your agricultural consulting needs");
    
    seed= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many seeds will you be planting?"));

    water= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many gallons of water will you need?"));

    acreage= Integer.parseInt (
        JOptionPane.showInputDialog(
        null, "How many acres of land do you own?"));

	JOptionPane.showMessageDialog (
	null, "What you need is listed below"
	+ Land.waterTotal(int gallonsneeded) + Land.seedTotal(int seedneeded)" .");
       } 
}//main

class Land {
    private int computeLand(int water, int seed, int acreage)
{
    int gallonsneeded;
    int seedneeded;
    int acreage;
}
    
    Land(int water, int seeds, int acreage)
    {
        gallonsneeded = water;
        seedneeded = seed;
        int acreage;
    }
    Land(int gallonsneeded);
    {
        this (1, gallonsneeded, 1);
    }
    Land(int seedneeded);
    {
        this (seedneeded, 1, 1);
    }
    Land(int acreage);
    {
        this (1, 1, acreage);
    }
    Land();
    {
        this (1, 1, 1);
    }//class
	private int waterTotal(int gallonsneeded, int acreage)
	{
	gallonsneeded= water * acreage;
	return gallonsneeded;
	}
	private int seedTotal(int seedneeded, int acreage)
	{
	seedneeded= seed * acreage;
	return seedneeded;
	}
	
}



It is probably something stupid simple. I don't define the parameters when making the new land object, I thought I defined them below but there is still something not right.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1