Simple transition of a variable to a new class, then output in Java

I asked this question in several ways, but the code is usually user-specific and I got a little lost. If I miss a simple clear and simple explanation, I'm sorry! I just need to understand this concept, and I lost the repetition that I saw. Therefore, I have simplified my own problem as much as possible in order to understand the essence of the problem.

The goal is to have a main class that I request for variables, and then these variables entered by the user are evaluated by the method in a separate class with a message return depending on the variables.

    import java.io.*;


    public class MainClass {

       public static void main(String[] args) {

          InputStreamReader input = new InputStreamReader(System.in);
          BufferedReader reader = new BufferedReader(input);   

          String A;
          String B;

          try {

               System.out.println("Is A present?");
               A = reader.readLine();

               System.out.println("Is B present?");
               B = reader.readLine();

               Assess test = new Assess();

} catch (IOException e){
  System.out.println("Error reading from user");
  }
 }
}

And the method I'm trying to use:

public class Assess extends MainClass {

 public static void main(String[] args) {

  String A = MainClass.A;
  String B = MainClass.B;

  if ((A.compareToIgnoreCase("yes")==0) && 
     ((B.compareToIgnoreCase("yes")==0) | (B.compareToIgnoreCase("maybe")==0))) 
         {
         System.out.println("Success!");
         }

  else {
    System.out.println ("Failure");
       }
}


}

, , , , , , , . , , , . , ; , , , , , , .

, , ​​ , ( , ... ? - , ).

+4
5

,

, :

Assess assess = new Assess(A, B);

:

Assess assess = new Assess();
assess.setA(A);
assess.setB(B);

""

public Assess(String A, String B)

public void setA(String A)
public void setB(String B)

, , .

.

Assess.java

public class Assess {

    private a;
    private b;

    public Assess(String a, String b) {
        this.a = a;
        this.b = b;
    }

    public boolean check() {
         if ((A.compareToIgnoreCase("yes")==0) && 
             ((B.compareToIgnoreCase("yes")==0) ||
             (B.compareToIgnoreCase("maybe")==0))) 
         {
             System.out.println("Success!");
             return true;
         } else {
             System.out.println ("Failure");
             return false;
         }

MainClass.java

public class MainClass {

    public static void main(String[] args) {

      InputStreamReader input = new InputStreamReader(System.in);
      BufferedReader reader = new BufferedReader(input);   

      String A;
      String B;

      try {

           System.out.println("Is A present?");
           A = reader.readLine();

           System.out.println("Is B present?");
           B = reader.readLine();

           Assess test = new Assess(A, B);

           boolean isBothPresent = test.check(); 
           // ................

      } catch (IOException e){
          System.out.println("Error reading from user");
      }
 }
+4

, .

. assess, , , :

public int assess(String valueToAssess)

, , , valueToAssess, , . , i int,

return i;

; .

, , . , , , .

...
String a = reader.readLine();
int answer = assess(a);
System.out.println("I've decided the answer is " + answer);

, ?

+1

. , {class}. {Member} (.. MainClass.A), .

public class MainClass {
    public static String A;
    public static String B;
    ...
}
public class Subclass {
    public static void main(String[] args) {
        // You can access MainClass.A and MainClass.B here
    }
}

, , , ,

public class MainClass {
    public String A;
    public String B;
    public static void main(String[] args) {
        // Manipulate A, B, assign values, etc.
        Assess assessObject = new Assess(A, B);
        if (assessObject.isValidInput()) {
            System.out.println("Success!");
        } else {
            System.out.println("Success!");
        }
    }
}
public class Assess {
    String response1;
    String response2;
    public Assess (String A, String B) {
        response1 = A;
        response2 = B;
    }
    public boolean isValidInput() {
        // Put your success/fail logic here
        return (response1.compareToIgnoreCase("yes") == 0);
    }
}
+1

. . , "". .    MainClass   {        public static void main (String [] Args)        {             ns = new Assessment();            ns.setterMethod( );        }   }

0

100% , , , . ...

, . MainClass

public static String A, B;

(MainClass ). , , . , .

public String A, B;    // Bad practice, who modified these?
protected String A, B;

, , , "accessors" (getters and setters). , , , , , .., . "A" - , , - .

private String A, B;

public setA(String newValue) {
   A = newValue;
}
public String getA() {
   return A;
}

...

: " ", , , , . , - , .

, , . , .

, , - , . MainClass , "MainClass a Assessment'or", ( ) "" ( "" ):

super.setA(local_a);
super.setB(local_b);
super.assess();

super.assess(A, B);
0

Source: https://habr.com/ru/post/1656730/


All Articles