Reputation: 502
I am writing a code on computerization of health records and making a program. But when i ran it ,it only gives the output:-
The total years of that person is 21613
BMI values Under weight: less than 18.5 normal: between 18.5 and 24.9 Overweight: between 25 and 29.9 Obese: 30 or greater Enter the value of Height and Weight
but all my other methods are not running and constructor as well, please just give me the idea how to solve this? My program is :-
import java.util.Scanner;
class Starter{
private String FirstName;
private String LastName;
private String Gender;
private int DOB;
private int MOB;
private int YOB;
private double Height; //inches
private double Weight; //KG.
String greeting=FirstName+LastName+Gender;
double BMI1;
public Starter(){
Scanner input=new Scanner(System.in);
System.out.println("Enter all the required data");
FirstName=input.nextLine();
LastName=input.nextLine();
Gender=input.nextLine();
DOB=input.nextInt();
MOB=input.nextInt();
YOB=input.nextInt();
}
public void setFirstName(String Fname){
FirstName=Fname;
}
public void setLastName(String Lname){
LastName=Lname;
}
public void setGender(String G){
Gender=G;
}
public void setBirth(int Day,int Month,int Year){
DOB=Day;
MOB=Month;
YOB=Year;
}
public String getFirstName(){
return FirstName;
}
public String getLastName(){
return LastName;
}
public String getGender(){
return Gender;
}
public int getDOB()
{
return DOB;
}
public int getMOB(){
return MOB;
}
public int getYOB(){
return YOB;
}
public void BMI1(){
System.out.println("Enter the value of Height and Weight");
Scanner input1=new Scanner(System.in);
Height=input1.nextFloat();
Weight=input1.nextFloat();
BMI1=Weight/(Height*Height);
System.out.println("The BMI value is :\t"+BMI1);
}
public void getYears(){
int TotalYear=2014-YOB;
int TotalMonth=12-MOB;
int TotalDay=31-DOB;
System.out.printf("The total years of that person is %d-%d-%d",TotalYear,TotalMonth,TotalDay);
System.out.println("\n");
}
public void BMI(){
System.out.println("BMI values");
System.out.println("Under weight:\tless than 18.5\n normal:\tbetween 18.5 and 24.9\nOverweight:\tbetween 25 and 29.9\nObese:\t30 or greater");
}
}
public class HeartRates {
public static void main(String[] args) {
Starter getvalues2=new Starter();
getvalues2.setFirstName("Sachin");
getvalues2.setLastName("Godara");
getvalues2.setGender("Male");
getvalues2.setBirth(18,6,1993);
getvalues2.getFirstName();
getvalues2.getLastName();
getvalues2.getGender();
getvalues2.getDOB();
getvalues2.getMOB();
getvalues2.getYOB();
getvalues2.getYears();
getvalues2.BMI();
getvalues2.BMI1();
}
}
MY OUTPUT SHOULD BE :-
Enter all the required data
sam
godara
male
18
6
1993
The total years of that person is 21-6-13
BMI values
Under weight: less than 18.5
normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Enter the value of Height and Weight
2
78
The BMI value is : 19.5
Sachin
godara
male
1861993
Upvotes: 0
Views: 555
Reputation: 2664
You are missing a constructor. If this :
void starter(){
Scanner input=new Scanner(System.in);
System.out.println("Enter all the required data");
FirstName=input.nextLine();
LastName=input.nextLine();
Gender=input.nextLine();
DOB=input.nextInt();
MOB=input.nextInt();
YOB=input.nextInt();
}
is meant as a constructor then its wrong. Even though a constructor is a void method, it is not declared like that. Change it to:
public Starter(){
Scanner input=new Scanner(System.in);
System.out.println("Enter all the required data");
FirstName=input.nextLine();
LastName=input.nextLine();
Gender=input.nextLine();
DOB=input.nextInt();
MOB=input.nextInt();
YOB=input.nextInt();
}
Update 1
These methods:
getvalues2.getFirstName();
getvalues2.getLastName();
getvalues2.getGender();
getvalues2.getDOB();
getvalues2.getMOB();
getvalues2.getYOB();
getvalues2.getYears();
return a value. That doesn't mean they have to print it..
Try
system.out.println(getvalues2.getFirstName());
system.out.println(getvalues2.getLastName());
....//etc
Update 2
also why it is giving The total years of that person is 21613 in output
This is happening because of this:
System.out.printf("The total years of that person is %d%d%d",TotalYear,TotalMonth,TotalDay);
%d%d%d
means that printf
expects 3 ints
as arguements and will place then next to each other.
I think this will make more sense to you:
`%d%d%d` output `123`
`%d %d %d` output `1 2 3`
`%d-%d-%d` output `1-2-3`
So you can change it appropriatelly.
Update 3
public void BMI1(){
System.out.println("Enter the value of Height and Weight");
Scanner input1=new Scanner(System.in);
Height=input1.nextFloat();
Weight=input1.nextFloat();
BMI1=Weight/(Height*Height);
System.out.println("The BMI value is :\t"+BMI1);
}
From your comment i am assuming that your porgramm is hanging here. This method is expecting you to input 2 float
s before printing out something. So if you don't enter something it won't get the to the System.out.println("The BMI value is :\t"+BMI1);
Upvotes: 2
Reputation: 393851
If this method is supposed to be the constructor :
void starter(){
It should be :
public Starter(){
Otherwise, it's just a regular class method, and won't be executed when you call new Starter()
.
A constructor doesn't have a return type, and Java is case sensitive.
EDIT :
Change :
System.out.printf("The total years of that person is %d%d%d",TotalYear,TotalMonth,TotalDay);
to :
System.out.printf("The total years of that person is %d %d %d",TotalYear,TotalMonth,TotalDay);
In order to make the output more readable.
Upvotes: 1