Question

I am a beginner and I am trying to understand what is static, private, public. Please see the following example written by me. It works, but I have very big doubts whether this is a correct way of defining variables and methods. Thanks in advance!

import java.util.Scanner;
import java.util.Date; 
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Biorhytm {

private static String nameOne;
private static String nameTwo;
private static String dobOneIn;
private static String dobTwoIn;
private static Date dobOne;
private static Date dobTwo;
static int diff;

public static Date getDobOne() {
    return dobOne;
    }

public static void setDobOne(Date dobOne) {
    Biorhytm.dobOne = dobOne;
    }

public static Date getDobTwo() {
    return dobTwo;
    }

public static void setDobTwo(Date dobTwo) {
    Biorhytm.dobTwo = dobTwo;
    }

public static String getDobOneIn() {
    return dobOneIn;
    }

public static void setDobOneIn(String dobOneIn) {
    Biorhytm.dobOneIn = dobOneIn;
    }

public static String getDobTwoIn() {
    return dobTwoIn;
    }

public static void setDobTwoIn(String dobTwoIn) {
    Biorhytm.dobTwoIn = dobTwoIn;
    }

public static String getNameOne() {
    return nameOne;
    }

public static void setNameOne(String nameOne) {
    Biorhytm.nameOne = nameOne;
    }

public static String getNameTwo() {
    return nameTwo;
    }

public static void setNameTwo(String nameTwo) {
    Biorhytm.nameTwo = nameTwo;
    }

public static int diffCalc() {
    return diff = Math.abs((int)((getDobOne().getTime() - getDobTwo().getTime()) / (24 * 60 * 60 * 1000)));
}

public static void main(String[] args) {

        float physicalBio;
        float emotionalBio;
        float intellectualBio;
        boolean validEntry;

        Scanner input = new Scanner(System.in);

        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
        SimpleDateFormat format2 = new SimpleDateFormat("EEEE, MMMM d, yyyy", java.util.Locale.US);

        System.out.println("Enter name of first person!");
        setNameOne(input.nextLine());

            if (getNameOne().equals("")) {
                setNameOne("first person");
            }

            System.out.println("Enter name of second person!");
            setNameTwo(input.nextLine());

            if (getNameTwo().equals("")) {
                setNameTwo("second person");
            }

         do {
                try {
                    System.out.println("Enter date of birth of " + getNameOne() + "! (MM/DD/YYYY)");
                    setDobOneIn(input.nextLine());
                    setDobOne(format.parse(getDobOneIn()));
                    validEntry = true;
                    }
                catch (ParseException e) {
                    validEntry = false;
                     }
            } while (!validEntry);

         do {
                try {
                    System.out.println("Enter date of birth of " + getNameTwo() + "! (MM/DD/YYYY)");
                    setDobTwoIn(input.nextLine());
                    setDobTwo(format.parse(getDobTwoIn()));
                    validEntry = true;
                    }
                catch (ParseException e) {
                    validEntry = false;
                     }
            } while (!validEntry);

        System.out.println();
        System.out.println("DOB of " + getNameOne() + ": " + format2.format(getDobOne()) + ".");
        System.out.println("DOB of " + getNameTwo() + ": " + format2.format(getDobTwo()) + ".");
        System.out.println("Difference between DOBs (days): " + diffCalc() + ".");

        physicalBio = diffCalc() % 23;
        emotionalBio = diffCalc() % 28;
        intellectualBio = diffCalc() % 33;

        physicalBio /= 23;
        emotionalBio /= 28;
        intellectualBio /= 33;

        if (physicalBio > 0.5) {
            physicalBio = 1 - physicalBio;
        }

        if (emotionalBio > 0.5) {
            emotionalBio = 1 - emotionalBio;
        }

        if (intellectualBio > 0.5) {
            intellectualBio = 1 - intellectualBio;
        }

        physicalBio = 100 - (physicalBio * 100);
        emotionalBio = 100 - (emotionalBio  * 100);
        intellectualBio = 100 - (intellectualBio * 100);

        System.out.println("Physical compatibility: " + java.lang.Math.round(physicalBio) + " %.");
        System.out.println("Emotional compatibility: " + java.lang.Math.round(emotionalBio) + " %.");
        System.out.println("Intellectual compatibility: " + java.lang.Math.round(intellectualBio) + " %.");

}

}
Was it helpful?

Solution

You'd rather have your Biorhythm class be something representing the data about a single person. So you'd create two instances of it (call them "one" and "two", say) and make them non-static. It would have instance variables, not static variables, representing name and date of birth.

class Biorhythm {
    private Date dob;
    private String name;

    Biorhythm(String name, Date dob) {
        this.name = name;
        this.dob = dob;
    }

    public String getName() {
      return name;
    }

    public Date getDob() {
      return dob;
    }
}

public static void main(String[] args) {
    Date onedob = /* implementation omitted */
    Biorhythm one = new Biorhythm("maxval", onedob);
    System.out.println("one: name=" + one.getName() + " date=" + one.getDob());
    /* and so forth */
}

You don't really have a need for setXXX() methods because these values aren't probably going to change in your program.

Now create two instances of this class in your main() method. I'll leave the implementation of the calculation methods as an exercise for the time being, since there would be several decent designs for implementing them (in terms of the object-oriented question you asked).

OTHER TIPS

First let me explain what these keywords are- private,default,protected.public are ACCESS SPECIFIERS. public-as the word says ,can be accessed everywhere and all members can be public. protected-can be accessed outside the package in case of inheritance. default-access able within the package and all members can be default. private-scope lies within the class,cant be inherited. And remember these can be used with variables,methods,even classes.

static-can be used when there is no need to invoke methods or access variables with objects. static members doesn't belong to object and initialised at the time of loading a class. for ex:

class Converter{
    public static long convert(long val){
    return val;
    }
    class User{
    long value=Converter.convert(500);//calling convert with class name}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top