Nate Craft
Nate Craft

Reputation: 21

Set and get with arguments in Java

I new to java, still trying to get down arguments and passing info. I am writing a blood pressure program for school and have some issue passing info from one class to another.

I have a fully functioning system to take in the user info in one class and have to set up another to check if the average is above or below range. Now, the range is easy, but the passing of info is another thing.

Here is part of my program (in the class PressureInput) and where my issues start:

public void setSystolic(int sys)
{
    sys = sysAvrg;
}

So, assuming the avrgSys has a number (it does), I then want to pass the info to the other class (BPChecker).

I don't feel like I'm doing this right, or at least, not in such a way as to facilitate passing the 'int' of sysAvrg from the class its in into another class (BPChecker).

I'm not sure whether to use a getSystolic since I'm not sure what the return would be.

I can't just initialize sys in the other class (BPChecker) without giving sys a value (which defeats the purpose), but it keeps telling me to.

In the end, I need to move the number of avrgSys into BPChecker without rewriting the whole program. So far, I keep getting a lot of 0s or errors...

Any help is appreciated, though my newness may have more complicated explanations go over my head (sorry to say).


So, here's the code i wrote. the ONLY thing I'm worried about is the very last part, the 'getSystolic' and its return. I need to send the info to another part of the program not in main or in this PressueInput (its BPChecker btw) and just banging my head against the problem.

Thank you for the input:

` import java.util.Scanner;

public class PressureInput 
{
private int sysInput;
private int diaInput;
private int sysAvrg;
private int diaAvrg;



public PressureInput()
{               
    sysInput = 0;
    diaInput = 0;
    sysAvrg = 0;
    diaAvrg = 0;

}

public void setSysPressure()
{
    sysInput = 0;
    while(sysInput <= 0 || sysInput >= 320)
    {
        Scanner cin = new Scanner(System.in); 
        System.out.println("Please enter a systolic reading> ");

        sysInput= cin.nextInt();

        System.out.println("You have entered " + sysInput + "\n");

        if(sysInput <=0 || sysInput >= 320)
        {
            System.out.println("You're either dead or entered" 
                               + " an error. Try again." + "\n");
        }
    }
    sysAvrg += sysInput;
}



public int getSysPressure()
{
    return sysInput;
}




public void setDiaPressure()
{
    diaInput = 0;
    while(diaInput <= 0 || diaInput >= 320)
    {
        Scanner cin = new Scanner(System.in); 
        System.out.println("Please enter a systolic reading> ");

        diaInput= cin.nextInt();

        System.out.println("You have entered " + diaInput + "\n");

        if(diaInput <=0 || diaInput >= 320)
        {
            System.out.println("You're either dead or entered" 
                               + " an error. Try again." + "\n");
        }
    }
    diaAvrg += diaAvrg;
}


public int getDiaPressure()
{
    return diaInput;
}


public void sysAvrgRead()
{
    sysAvrg = sysAvrg / 3;
    System.out.println("\n" + "The systolic averge is " + sysAvrg);
}


public void diaAvrgRead()
{
    diaAvrg = diaAvrg / 3;
    System.out.println("The diastolic averge is " + diaAvrg + "\n");
}


public void setSystolic(int sys)
{
    sysAvrg = sys;
}


public int getSystolic()
{
    return sys;
}  
} `

Upvotes: 0

Views: 83

Answers (2)

user2867798
user2867798

Reputation:

In Object-oriented programming, you can create an instance of an object in any class you want. In order to access class variables from other classes, you can use accessor methods.

i.e.

public class PressureInput {
  private static int sysAvrg;  

  public PressureInput(int sysAvrg){
    this.sysAvrg = sysAvrg;
  }

  public void setSystolic(int sys){
    this.sysAvrg = sys;
  }

  public int getSystolic() {
    return this.sysAvrg;
  } 

}

Upvotes: 2

You have a variable called sys so if you want to set it with the average create a setter Method:

public void setSystAve(float sysAvrgParameter){
   sysAvrg = sysAvrgParameter;
}

if you want to get the sysAvrg create a getter Method:

public float getSystAve(){
       return sysAvrgParameter;
    }

now somewhere in your code:

sys = yourObject.getSystAve();

Upvotes: 0

Related Questions