#Please help with how to do this exercise
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @sinful pasture! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
public int computeDiscountPercent()
{
int discount = 0;
if ((this.student)&&(this.age>=10)&&(this.age <= 20))
discount = 50;
return discount;
}
public boolean isStudent()
{
return this.student;
}
public int getAge()
{
return this.age;
}
You computeDiscountPercent function is correct but isStudent and age need to be returned like I mentioned above
@solid ferry I run the code and I put age = 12
but when I print The discount percent for this person is:" + discount + ".");
the discount is 0
it should be 50
did you also put person as a student ?
I have this ```public class Person {
int age;
double weight;
boolean student;
char gender;
public Person(int age, double weight, boolean student, char gender )
{
this.age = age;
this.weight = weight;
this.student = student;
this.gender = gender;
}```
No I am asking are you putting person as a student while giving input
Oh okay got it actually you need to create the object after taking the input and pass the variables in it
You are using default constructor object which have all values as default i.e. 0
Try creating the object after taking input by using this constructor
public Person(int age, double weight, boolean student, char gender )
{
this.age = age;
this.weight = weight;
this.student = student;
this.gender = gender;
}
@solid ferry I have put the constructor with 4 parameters
how come discount doesn't become 50
when i put age as 12
or sth between 10 to 20
bro you are using the default constructor for your object try this
System.out.print("Please enter the person's age:");
int age = scan.nextInt();
System.out.print("Please enter the person's weight:");
double weight = scan.nextDouble();
System.out.print("Is the person a student (true/false):");
boolean student = scan.nextBoolean();
System.out.print("Please enter the person's gender (M/F):");
char gender = scan.next().charAt(0);
System.out.println("Person: age: " + age + " weight: " + weight + "retired: " + student + "gender " + gender +"");
Person p1 = new Person(age,weight,student,gender);
int discount = p1.computeDiscountPercent();
p1.isStudent();
p1.getAge();
scan.close();
}
See here we are creating the object (p1) with the constructor having four parameters earlier you are creating the object using default constructor
I hope this make sense to you
@solid ferry why did you put the four variables in Person(age,weight,student,gender);
also are the methods for isStudent and getAge the correct approach according to the exercise
its confusing
Becuase we want to set the values of all these varaibles in Person class
Yes in this way they are correct
public boolean isStudent()
{
return this.student;
}
public int getAge()
{
return this.age;
}
Yup earlier you are using the object in which you are not passing the values so you are seeing zero as discount
okay ... i think i understand now
just wondering if the output console is suppose to show True later on
because the exercise says //returns true if this Person is a student and false otherwise
Yup it will return whatever the input you will give
But since you are taking input as boolean make sure to give input as 0 or 1
Or you can do something like this
System.out.print("Is the person a student (T/F):");
char isStudentIsPerson = scan.next().charAt(0);
boolean student = false;
if(isStudentIsPerson == 'T') {
student = true;
}
else {
student = false;
}
okay
Wait I'm editing that
We can do like this as well
So if you give input as T student variable value is assigned as true
Else false
Or if you want the boolean input you can simply do
System.out.print("Is the person a student (0/1):");
boolean student = scan.nextBoolean();
Let me know if it works or not
Yup
For me it's working didn't know why not working for you
public class Person {
int age;
double weight;
boolean student;
char gender;
// constructor with inputs to initialise all four instance variables
public Person(int age, double weight, boolean student, char gender) {
this.age = age;
this.weight = weight;
this.student = student;
this.gender = gender;
}
// default constructor
public Person() {
this.age = 0;
this.weight = 0;
this.student = false;
this.gender = 0;
}
public int computeDiscountPercent() {
int discount = 0;
if ((this.student) && (this.age >= 10) && (this.age <= 20))
discount = 50;
return discount;
}
public boolean isStudent() {
return this.student;
}
public int getAge() {
return this.age;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter the person's age:");
int age = scan.nextInt();
System.out.print("Please enter the person's weight:");
double weight = scan.nextDouble();
System.out.print("Is the person a student (T/F):");
char isStudentPerson = scan.next().charAt(0);
boolean student = false;
if(isStudentPerson == 'T') {
student = true;
}
else {
student = false;
}
System.out.print("Please enter the person's gender (M/F):");
char gender = scan.next().charAt(0);
System.out.println(
"Person: age: " + age + " weight: " + weight + "retired: " + student + "gender " + gender + "");
Person p1 = new Person(age,weight,student,gender);
int discount = p1.computeDiscountPercent();
System.out.println(discount);
System.out.println(p1.isStudent());
System.out.println(p1.getAge());
scan.close();
}
}
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.