1

So I have looked at numerous posts on classes, references and methods, set and get for a couple days now. I can't seem to comprehend how it all works.

Disclaimer yes this is for classwork, the poor example below is an attempt to illustrate. I would appreciate your help.

I already have a working (albeit very rudimentary and inefficient program). However everything I have is in my main method of my Primary class and I need to put part of it in a separate class and have it all still work.

With the main method the user inputs a clear text password (clearText) wherein a provided snippet of code hashes the password into (hashText). Which is then used for other things.

What I wish to accomplish is to separate the snippet of code that hashes the password out of the main method of my primary class, into a separate secondary class.

What I cannot figure out is how to do that. How to import clearText into secondary class, then output hashText back to the main method in the primary class.

Thank you.

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      System.out.print("Type Text");
      clearText = scnr.next();

   //Somehow export clearText to the secondary class
   //Somehow import hashText into the main method for further use

      System.out.println(hashText);
   }
}

public class Secondary {
   String hashText = ("");

   //Somehow import clearText value inputted but the user

   //here is where clearText gets hashed(I have that)

   //Somehow export hashText for use in the main method
}
1
  • Do you really need to use the "Secondary" class? If not, why not using just a simple method receiving the clearText and returning its hashed content. Commented Dec 14, 2018 at 14:47

5 Answers 5

1

Welcome to programming. One thing to think about here is it seems that you think your Secondary class is an entity that is immutable and runs alongside your program. It isn't. You're creating an object that contains data and functionality that you want to use elsewhere in your program.

Your Secondary object can be instantiated and then used to perform tasks. You could also do this from another method created inside of your base class, but that would have to be static as well.

I don't see much point in your secondary class containing an instance of the hash, and that question has been answered already. I would suggest you think about creating it as a service.

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      // Collect user input
      System.out.print("Type Text");
      clearText = scnr.next();

      // Create hash and display
      String hashText = HashService.generateHash(clearText);
      System.out.println(hashText);
   }
}

public class HashService {
  public static String generateHash(String fromText){
    // implementation here
    return null;
  }
}

Edit: It looks like someone erased the object answer. If you for some reason want to maintain your hashed password as an object you could do it like this

import java.util.*;
import java.io.*;

public class Primary {
   public static void main(String[] args) throws Exception {
      String clearText = ("");

      // Collect user input
      System.out.print("Type Text");
      clearText = scnr.next();

      // Create hash and display
      HashedPassword hash = new HashedPassword(clearText);
      String hashText = hash.getHashedPW();
      System.out.println(hashText);
   }
}

public class HashedPassword {
  private String hashedPW = ""
  public HashedPassword(String fromText){
    // Assign hashedPW with some logic
    hashedPW = fromText;
  }

  public getHashedPW(){
    return hashedPW;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, the top half of your response allowed me to do what I was trying to do. I was then able to modify it and implement it in my larger project.
0

Simple create a method in second class which accepts your cleartext and returns the hash value and then call this method on the second class's object from the main method

Comments

0
public static void main(String[] args) throws Exception {
    String clearText = ("");

    System.out.print("Type Text");
    clearText = scnr.next();

    // Somehow export clearText to the secondary class
    // Somehow import hashText into the main method for further use

    System.out.println(Secondary.stringToHash(clearText));
}

publicclass Secondary {

    public static long stringToHash(String input) {
        // some transformation
    }

    // Somehow import clearText value inputted but the user

    // here is where clearText gets hashed(I have that)

    // Somehow export hashText for use in the main method
}

This should work, but you don't really need another class, you can just create a static method.

Comments

0

You can do the following:

import java.util.*;
import java.io.*;

public class Primary {
    public static void main(String[] args) throws Exception {
        String clearText = ("");

        System.out.print("Type Text");
        clearText = scnr.next();

        String hashText = Secondary.hashText(clearText);

        System.out.println(hashText);
    }
}

public class Secondary {

    public static String hashText(String clearText) {
        // TODO return the hash
        return null;
    }

}

Or if you have some non-static thing in your secondary class, then you can make an instance of it (by Secondary secondary = new Secondary()) and then call secondary.hashText() (also make hashText non-static) :)

2 Comments

Thank you, I attempted to apply your suggestion unsuccessfully.
If you want you can give me more information about your code, so I can help better :)
0

So, if you need two separate classes keep in mind that each class has to be in separate files. YOu need to import the second class in the first one like this:

import YourPackageName.YourClassName;

then in the main class you can do like this:

public class Primary {
  public static void main(String[] args) throws Exception {
      String clearText = ("");

      System.out.print("Type Text");
      clearText = scnr.next();

   Secondary secondClass = new Secondary();
   String hashText = secondClass.hash(clearText);

      System.out.println(hashText);
   }
}

Meanwhile in Secondary calss:

public class Secondary {

   public String hash (String clearText){
      //doHash
   }

}

4 Comments

Classes definitely do not have to be in separate files. They don't even need to be outside of other classes.
I've misread the question, I thought he wanted them to be in separate files. Sorry
No problem just didn't want him getting confused.
Thank you, I did need it in separate files as well actually. However I did attempt to apply your suggestion unsuccessfully. That reflects more so on my ability to apply your response, than the response itself. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.