8 Replies - 256 Views - Last Post: 01 February 2013 - 09:17 PM Rate Topic: -----

#1 chizzain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-December 12

A Main Class?

Posted 31 January 2013 - 10:17 PM

How do I add a main class? Where? Here is my code


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;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: A Main Class?

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: A Main Class?

Posted 31 January 2013 - 11:29 PM

A main class contains:

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.
Was This Post Helpful? 0
  • +
  • -

#3 chizzain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-December 12

Re: A Main Class?

Posted 01 February 2013 - 06:16 PM

View Postfarrell2k, on 31 January 2013 - 11:29 PM, said:

A main class contains:

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.


Can't figure out where to add main() to my current code.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,576
  • Joined: 12-December 12

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;

Was This Post Helpful? 0
  • +
  • -

#5 chizzain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-December 12

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;
    }
}
}

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,576
  • Joined: 12-December 12

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.
Was This Post Helpful? 0
  • +
  • -

#7 chizzain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-December 12

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;
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 lbfb  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 31
  • Joined: 23-February 12

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 ;
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.
Was This Post Helpful? 0
  • +
  • -

#9 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: A Main Class?

Posted 01 February 2013 - 09:17 PM

You can also declare the main class as :

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1