Reputation: 23
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
}
Upvotes: 1
Views: 178
Reputation: 433
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;
}
}
Upvotes: 1
Reputation: 11
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
}
}
Upvotes: 0
Reputation: 847
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) :)
Upvotes: 0
Reputation: 165
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.
Upvotes: 0
Reputation: 986
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
Upvotes: 0