View Single Post
  #1 (permalink)  
Old 03-28-2008, 03:33 AM
oceansdepth oceansdepth is offline
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.
Reply With Quote
Sponsored Links