Results 1 to 2 of 2
Thread: Help with code (static error)
- 03-28-2008, 01:33 AM #1
Member
- Join Date
- Mar 2008
- Posts
- 5
- Rep Power
- 0
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.
- 03-28-2008, 04:32 AM #2
Similar Threads
-
Explicit static initialization with the static clause
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:07 PM -
Pls help with a code error.
By saytri in forum New To JavaReplies: 8Last Post: 12-24-2007, 08:10 PM -
error in code
By dirtycash in forum New To JavaReplies: 2Last Post: 12-06-2007, 11:40 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks