Hello Im new to java and Im having trouble getting my code to work. Im able to call some methods but others dont work right.
Here is what I have to do:
Create a class called myStuff that contains a constructor, getters and setters and a toString method. The private data members will be: firstName, age, shoeSize, favoriteSnack and favoriteMovie. Create a driver class that creates an instance of myStuff in main. Get the information from the user and “set” it into the object using the setter methods. Display the information to the screen using the toString method.
EX.
What is your name? Abigail
What is your age? 22
What is your favorite snack? Doritos
Output:
Abigail
Age: 22
Snack: Doritos
******Here is myStuff Class**********8
import java.util.Scanner;
public class myStuff {
private String name, newName, snack, newSnack;
private int age, newAge;
private double shoeSize;
Scanner in = new Scanner(System.in);
public myStuff()
{
this.name = name;
this.snack = snack;
this.age = age;
}
//methods
public String setName(){
System.out.println("What is your name?");
name = in.nextLine();
return name;
}
public void getName(){
name = newName;
}
public int setAge(){
System.out.println("What is your age?");
age = in.nextInt();
return age;
}
public void getAge(){
age = newAge;
}
public String setSnack(){
System.out.println("What is your favorite snack?");
snack = in.nextLine();
return snack;
}
public void getSnack(){
snack = newSnack;
}
public String toString(){
String result = Integer.toString(age);
return result;
}
}
**********DriverClass********************
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
myStuff obj_1;
obj_1 = new myStuff();
obj_1.setAge();
obj_1.setName();
obj_1.setSnack();
System.out.println(obj_1);
}
}
************************************************** ************************
My question is how do I store all the user input and Display them using the toString?
- I called the methods on the main class, but after I call setAge()method I can't input any other info.
THANKS FOR ANY HELP