import java.util.*;
class healthProfile {
private String first;
private String last;
private char gender;
private int birthMonth;
private int birthDay;
private int birthYear;
private double height;
private double weight;
public healthProfile (String f, String l, char g, int m, int d, int y, double h, double w) {
first = f;
last = l;
gender = g;
birthMonth = m;
birthDay = d;
birthYear = y;
height = h;
weight = w;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
public char getGender() {
return gender;
}
public int getBirthMonth() {
return birthMonth;
}
public int getBirthDay() {
return birthDay;
}
public int getBirthYear() {
return birthYear;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public void setFirst(String f) {
first = f;
}
public void setLast(String l) {
last = l;
}
public void setGender(char g) {
gender = g;
}
public void setBirthMonth(int n) {
birthMonth = n;
}
public void setBirthDay(int n) {
birthDay = n;
}
public void setBirthYear(int n) {
birthYear = n;
}
public void setHeight(double n) {
height = n;
}
public void getWeight(double n) {
weight = n;
}
public double calcBMI() {
return (weight * 703) / (height * height);
}
public int calcAge() {
Calendar cal = Calendar.getInstance();
int day, month, year;
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
year = cal.get(Calendar.YEAR);
if (month > birthMonth) {
return year - birthYear;
}
if (month == birthMonth && day >= birthDay) {
return year - birthYear;
}
return year - birthYear - 1;
}
public int calcMaxHR() {
return 220 - calcAge();
}
public double targetMinHR() {
return calcMaxHR() * .5;
}
public double targetMaxHR() {
return calcMaxHR() * .75;
}
}
A Main Class?
Page 1 of 18 Replies - 187 Views - Last Post: 01 February 2013 - 09:17 PM
#1
A Main Class?
Posted 31 January 2013 - 10:17 PM
How do I add a main class? Where? Here is my code
Replies To: A Main Class?
#2
Re: A Main Class?
Posted 31 January 2013 - 11:29 PM
A main class contains:
You can add a main() to that class, you can create another class with a main(). It's up to you.
public static void main(String[] args) {
}
You can add a main() to that class, you can create another class with a main(). It's up to you.
#3
Re: A Main Class?
Posted 01 February 2013 - 06:16 PM
#4
Re: A Main Class?
Posted 01 February 2013 - 06:22 PM
You could bung it in after your declarations, after this line:
private double weight;
#5
Re: A Main Class?
Posted 01 February 2013 - 06:30 PM
Like this? I'm getting an error after the main() line now lol
import java.util.*;
class healthProfile {
private String first;
private String last;
private char gender;
private int birthMonth;
private int birthDay;
private int birthYear;
private double height;
private double weight;
public static void main(String[] args) {
public healthProfile(String f, String l, char g, int m, int d, int y, double h, double w) {
first = f;
last = l;
gender = g;
birthMonth = m;
birthDay = d;
birthYear = y;
height = h;
weight = w;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
public char getGender() {
return gender;
}
public int getBirthMonth() {
return birthMonth;
}
public int getBirthDay() {
return birthDay;
}
public int getBirthYear() {
return birthYear;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public void setFirst(String f) {
first = f;
}
public void setLast(String l) {
last = l;
}
public void setGender(char g) {
gender = g;
}
public void setBirthMonth(int n) {
birthMonth = n;
}
public void setBirthDay(int n) {
birthDay = n;
}
public void setBirthYear(int n) {
birthYear = n;
}
public void setHeight(double n) {
height = n;
}
public void getWeight(double n) {
weight = n;
}
public double calcBMI() {
return (weight * 703) / (height * height);
}
public int calcAge() {
Calendar cal = Calendar.getInstance();
int day, month, year;
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
year = cal.get(Calendar.YEAR);
if (month > birthMonth) {
return year - birthYear;
}
if (month == birthMonth && day >= birthDay) {
return year - birthYear;
}
return year - birthYear - 1;
}
public int calcMaxHR() {
return 220 - calcAge();
}
public double targetMinHR() {
return calcMaxHR() * .5;
}
public double targetMaxHR() {
return calcMaxHR() * .75;
}
}
}
#6
Re: A Main Class?
Posted 01 February 2013 - 06:40 PM
You haven't closed your main function with a matching closing curly bracket }. Every opening bracket must have a corresponding closing bracket.
#7
Re: A Main Class?
Posted 01 February 2013 - 07:00 PM
Ok so like this? I am now running it but it doesn't prompt the user for the information I want. Not sure what to do.
import java.util.*;
class healthProfile {
private String first;
private String last;
private char gender;
private int birthMonth;
private int birthDay;
private int birthYear;
private double height;
private double weight;
public static void main(String[] args) {
}
public healthProfile(String f, String l, char g, int m, int d, int y, double h, double w) {
first = f;
last = l;
gender = g;
birthMonth = m;
birthDay = d;
birthYear = y;
height = h;
weight = w;
}
public String getFirst() {
return first;
}
public String getLast() {
return last;
}
public char getGender() {
return gender;
}
public int getBirthMonth() {
return birthMonth;
}
public int getBirthDay() {
return birthDay;
}
public int getBirthYear() {
return birthYear;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public void setFirst(String f) {
first = f;
}
public void setLast(String l) {
last = l;
}
public void setGender(char g) {
gender = g;
}
public void setBirthMonth(int n) {
birthMonth = n;
}
public void setBirthDay(int n) {
birthDay = n;
}
public void setBirthYear(int n) {
birthYear = n;
}
public void setHeight(double n) {
height = n;
}
public void getWeight(double n) {
weight = n;
}
public double calcBMI() {
return (weight * 703) / (height * height);
}
public int calcAge() {
Calendar cal = Calendar.getInstance();
int day, month, year;
day = cal.get(Calendar.DATE);
month = cal.get(Calendar.MONTH) + 1;
year = cal.get(Calendar.YEAR);
if (month > birthMonth) {
return year - birthYear;
}
if (month == birthMonth && day >= birthDay) {
return year - birthYear;
}
return year - birthYear - 1;
}
public int calcMaxHR() {
return 220 - calcAge();
}
public double targetMinHR() {
return calcMaxHR() * .5;
}
public double targetMaxHR() {
return calcMaxHR() * .75;
}
}
#8
Re: A Main Class?
Posted 01 February 2013 - 08:45 PM
None of your code actually asks for any input. What you could do is to add some code like this inside your main method ;
(you will have to change your calcBMI method to accept height and weigh as parameters). When you run the code, it should has for height, then weight and then output the calculated BMI. Obviously you will want to do more than this, but this should get you started. Also, you should look up how to use try-catch to ensure you get the input you need from the users.
System.out.println("Please enter your height");
Scanner scanner = new Scanner(System.in);
double yourheight = scanner.nextDouble();
System.out.println("Please enter your weight");
Scanner scanner1 = new Scanner(System.in);
double yourweight = scanner1.nextDouble();
double yourBMI = calcBMI(yourweight, yourheight);
System.out.println("Your BMI is " + yourBMI);
(you will have to change your calcBMI method to accept height and weigh as parameters). When you run the code, it should has for height, then weight and then output the calculated BMI. Obviously you will want to do more than this, but this should get you started. Also, you should look up how to use try-catch to ensure you get the input you need from the users.
#9
Re: A Main Class?
Posted 01 February 2013 - 09:17 PM
You can also declare the main class as :
OR
However the name of the file must be Program
public class Program{
static public void main(String... args) {
// CODE FOR EXECUTION
}
}
OR
class Program{
static public void main(String... args) {
// CODE FOR EXECUTION
}
}
However the name of the file must be Program
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|