Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-28-2008, 02:33 AM
Member
 
Join Date: Mar 2008
Posts: 5
oceansdepth is on a distinguished road
Help with code (static error)
I have two classes: farm and cow. Class cow compiled fine but class farm is giving me this error:

1) " non-static method buildCow() cannot be referenced from a static context"

2) I also need to write a for loop to report on the herd after I loaded my array.

3) I also have to write a heading, and use the toString method to report, then write a summary.


I really mostly need help on number 1. The rest I'm trying to work on now... Thank you.

CODE:



import java.util.*;
import java.io.*;
import java.lang.*;

public class cow
{

private String name;
private String breed;
private int weight;
private char gender;


//define the class constructor
public cow()
{
this.name = "";
this.breed = "";
this.weight = 0;
this.gender = ' ';
return;
}
//pre: String newname, String newbreed, int newweight, char newgender
//post: void (nothing)

public cow (String newname, String newbreed, int newweight, char newgender)
{
name = newname;
breed = newbreed;
weight = newweight;
gender = newgender;
return;

}




//pre: object cow
//post: String name

public String getName()
{
return name;
}//***************************end of getName


//pre: object cow
//post: String breed
public String getBreed()
{

return breed;
}//***************************end of getBreed

//pre: object cow
//post: int weight
public int getWeight()
{
return weight;
}//***************************end of getWeight

//pre: object cow
//post: char gender
public char getGender()
{
return gender;
}//**************************end of getGender

//pre: object
//post: String description

public String toString()
{

String description = name+ " is a " + breed + " cow that weighs " +weight+ " lbs. Gender: " + gender;
return description;
}//*************************end of toString

//pre: String newName
//post: nothing

public void setName(String newName)
{
name = newName;
return;
}//*************************end of setName
//pre: String newBreed
//post: nothing
public void setBreed(String newBreed)
{
breed = newBreed;
return;
}//*************************end of setBreed

//pre: int newWeight
//post: nothing
public void setWeight(int newWeight)
{
weight = newWeight;
return;
}//*************************end of newWeight

//pre: char newGender
//post: nothing
public void setGender(char newGender)
{
gender = newGender;
return;
}//*************************end of newGender

//pre: nothing
//post: cow
public cow buildCow ()
{
Scanner cscan = new Scanner(System.in);
cow c1 = new cow();
System.out.println("What is the name of the cow?: ");
name = cscan.nextLine();
System.out.println("What is the breed of the cow?: ");
breed = cscan.nextLine();
System.out.println("What is the weight of the cow? ");
weight = cscan.nextInt();
System.out.println("What is the gender of the cow? ");
gender = cscan.nextLine().charAt(0);

c1.setName(name);
c1.setBreed(breed);
c1.setWeight(weight);
c1.setGender(gender);

return c1;
}

}
---------------------------------------------------------------------------------------------

import java.util.*;
import java.io.*;
import java.lang.*;
public class farm
{

Scanner cscan = new Scanner(System.in);
int newcows = 0;
public cow cowList()
{
System.out.println("How many cows do you wish to create?: ");
newcows = cscan.nextInt();

cow[] cowList = new cow[newcows];
for(int index = 0; index < newcows; index ++)
{
cow c1 = cow.buildCow();
}


}

}
i highlighted in red what gave me the prob.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 05:32 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Try
Code:
cow c1 = new cow(); cow c2 = c1.buildCow(); // or cow c = new cow().buildCow();
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit static initialization with the static clause Java Tip java.lang 0 04-17-2008 12:07 AM
Pls help with a code error. saytri New To Java 8 12-24-2007 09:10 PM
error in code dirtycash New To Java 2 12-07-2007 12:40 AM
Error: Non-static method append(char) cannot be referenced from a static context paul Advanced Java 1 08-07-2007 06:05 AM
Error: non-static variable height cannot be referenced from a static context at line fernando AWT / Swing 1 08-01-2007 10:25 PM


All times are GMT +3. The time now is 08:24 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org