Hello, I am a Newbie at JAVA programming language and i have followed a lot of tutorials in Java But i find difficult to make it work this piece of code to Object Oriented Programming. I have 2 programs the first is the procedural way. I used an array of strings to store two dates.toString (short, long) and two Hashes(dates.toString[long,short])
import java.math.BigInteger;
import java.time.format.DateTimeFormatter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import org.hsqldb.Trace;
public class Hashing {
private LocalDateTime justNow;
/**
* Full Date and Time
*/
private String fullDate;
/**
* Current date and time cropped
*/
private String currentDateCropped;
/**
* Hashing Function of FullDate HASHED
*/
private String showFullDate;
/**
* Hashing Function of DateCropped HASHED
*/
private String showCurrentDateCropped;
/**
* Arguments:
* Refactoring procedure below with Object oriented Programming
* @return String Array of 2 results [0] Limited Date [1]
* FullDate
*/
public string getDate() {
setFullDateNow();
System.out.println(getFullDate().toString(true));
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("dd/MM/yyyy | HH");
LocalDateTime now;
now = LocalDateTime.now();
System.out.println(dtf.format(now));
System.out.println(dtfFull.format(now));
Date date = java.util.Calendar.getInstance().getTime();
String[] dateStr = new String[2];
dateStr[0] = " ";
dateStr[1] = " ";
dateStr[0] = dtf.format(now);
dateStr[1] = dtfFull.format(now);
return dateStr;
}
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 32) {
hexString.insert(0, '0');
}
return hexString.toString();
}
// Driver code
public Hashing() {
try {
//remove
System.out.println("HashCode Generated by SHA-256 for:");
String[] currentDate = getDate();
String[] result = new String[4];
result[0] = currentDate[0];
result[1] = currentDate[1];
result[2] = toHexString(getSHA(currentDate[0]));
result[3] = toHexString(getSHA(currentDate[1]));
System.out.println("\n" + currentDate[0] + " : " + toHexString(getSHA(currentDate[0])));
} // For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
return myString;
}
public static void main(String args[]) {
Hashing();
}
}
and the other refactored chunk of Code is:
import java.math.BigInteger;
import java.time.format.DateTimeFormatter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import org.hsqldb.Trace;
public class Hashing {
private LocalDateTime justNow;
/**
* Full Date and Time
*/
private String fullDate;
/**
* Current date and time cropped
*/
private String currentDateCropped;
/**
* Hashing Function of FullDate HASHED
*/
private String varFullDateHash;
/**
* Hashing Function of DateCropped HASHED
*/
private String varCurrentDateCroppedHash;
/**
* Initializer of what time is now on the constructor (Netbeans Warning)
*/
public void initComponentstatic() {
setFullDateNow();
}
/**
* Getter for getting Full Date
*
* @return LocalDateTime Just NOW!
*/
public LocalDateTime getFullDate() {
LocalDateTime inner = this.justNow;
return inner;
}
/**
* Setter for Setting a Custom Date to private variable justNow
*
* Void justNOW=CustomDate
*/
public void setFullDate(LocalDateTime MyDate) {
LocalDateTime inner = MyDate;
this.justNow = inner;
}
/**
* Setter for Setting a Current Date to private variable justNow
*
* Void justNOW=now()
*/
public void setFullDateNow() {
LocalDateTime now;
now = LocalDateTime.now();
this.justNow = now;
}
/**
* Setter for Setting a Current Date to private variable justNow
*
* Void justNOW=now()
*/
//@Override It doesn't Need to be overriden because it has a a parameter (Boolean)
public String toString(boolena full) {
DateTimeFormatter dtf;
String TEMP;
LocalDateTime currentDate;
if (full) {
dtf = DateTimeFormatter.ofPattern(
"dd/MM/yyyy | HH:mm:ss:SS");
} else {
dtf = DateTimeFormatter.ofPattern(
"dd/MM/yyyy | HH");
}
/** This doesn't work
try {
if (this.justNow = null) {
System.out.println("justNow Inited");
} else {
System.out.println("justNow Empty");
}
} catch (Exception e) {
}*/
currentDate = this.justNow;
TEMP = currentDate.format(dtf);
return TEMP; //To change body of generated methods, choose Tools | Templates.
}
/**
* Arguments:
* Refactoring procedure below with Object oriented Programming
* The variables are stored in this new OOP code as private variables, and accessed through getters and setters.
*/
/**
* public string getDate() {
* setFullDateNow();
*
* System.out.println(getFullDate().toString(true));
* /**
* DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy |
* HH");
* DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("dd/MM/yyyy |
* HH:mm:ss:SS");
*
* LocalDateTime now;
* now = LocalDateTime.now();
*
* System.out.println(dtf.format(now));
* System.out.println(dtfFull.format(now));
* Date date = java.util.Calendar.getInstance().getTime();
* String[] dateStr = new String[2];
* dateStr[0] = " ";
* dateStr[1] = " ";
* dateStr[0] = dtf.format(now);
* dateStr[1] = dtfFull.format(now);
*
* return dateStr;
*
* }
*/
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 32) {
hexString.insert(0, '0');
}
return hexString.toString();
}
// Driver code
public Hashing() {
try {
//remove
initComponentstatic();
DateTimeFormatter dtf;
dtf = DateTimeFormatter.ofPattern(
"dd/MM/yyyy | HH:mm:ss:SS");
//System.out.println(getFullDate().toString(true));
String myString = getFullDate().toString(true);
System.out.println(myString);
fullDate=getFullDate().toString(true);
currentDateCropped=getFullDate().toString(false);
varFullDateHash=toHexString(getSHA(fullDate));;
varCurrentDateCroppedHash=toHexString(getSHA(fullDateCropped));;;
System.out.println("\n" + fullDate + " : " + varFullDateHash);
System.out.println("\n" + currentDateCropped + " : " + varCurrentDateCroppedHash);
*/
} // For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
return myString;
}
public static void main(String args[]) {
Hashing();
}
}
the idea is to translate the procedural code to 4 classes' variables. my problem is why my toString(boolean) of the class hashing doesn't work should i Override, or shouldn't I? I don't know What is wrong with my code, it says current argument (no arguments Required) doesnt match (1 argument boolean) and i can´t get it to work. I would like if Someone could give me a clue what am i doing wrong so i can solve it. If i override the system says it' s not needed because of the argument required by toString(). Thanks For any help, it would be much appreciated. and if by the way there is some books about Object Oriented Programming that anyone can suggest i will Thank a lot. Regards From Argentina.
I have fixed all typos and mistakes there were in your procedural code.
Now it compiles and run, but please revise it so you can be sure it works as expected.
Main.java (Change again to Hashing.java)
import java.math.BigInteger;
import java.time.format.DateTimeFormatter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
public class Main {
private LocalDateTime justNow;
/**
* Full Date and Time
*/
private String fullDate;
/**
* Current date and time cropped
*/
private String currentDateCropped;
/**
* Hashing Function of FullDate HASHED
*/
private String showFullDate;
/**
* Hashing Function of DateCropped HASHED
*/
private String showCurrentDateCropped;
/**
* Arguments:
* Refactoring procedure below with Object oriented Programming
* @return String Array of 2 results [0] Limited Date [1]
* FullDate
*/
public String[] getDate() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter dtfFull = DateTimeFormatter.ofPattern("dd/MM/yyyy | HH");
LocalDateTime now;
now = LocalDateTime.now();
System.out.println(dtf.format(now));
System.out.println(dtfFull.format(now));
Date date = java.util.Calendar.getInstance().getTime();
String[] dateStr = new String[2];
dateStr[0] = " ";
dateStr[1] = " ";
dateStr[0] = dtf.format(now);
dateStr[1] = dtfFull.format(now);
return dateStr;
}
public static byte[] getSHA(String input) throws NoSuchAlgorithmException {
// Static getInstance method is called with hashing SHA
MessageDigest md = MessageDigest.getInstance("SHA-256");
// digest() method called
// to calculate message digest of an input
// and return array of byte
return md.digest(input.getBytes(StandardCharsets.UTF_8));
}
public static String toHexString(byte[] hash) {
// Convert byte array into signum representation
BigInteger number = new BigInteger(1, hash);
// Convert message digest into hex value
StringBuilder hexString = new StringBuilder(number.toString(16));
// Pad with leading zeros
while (hexString.length() < 32) {
hexString.insert(0, '0');
}
return hexString.toString();
}
// Driver code
public void hash() {
try {
//remove
System.out.println("HashCode Generated by SHA-256 for:");
String[] currentDate = getDate();
String[] result = new String[4];
result[0] = currentDate[0];
result[1] = currentDate[1];
result[2] = toHexString(getSHA(currentDate[0]));
result[3] = toHexString(getSHA(currentDate[1]));
System.out.println("\n" + currentDate[0] + " : " + toHexString(getSHA(currentDate[0])));
} // For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
System.out.println("Exception thrown for incorrect algorithm: " + e);
}
}
public static void main(String args[]) {
Main e = new Main();
e.hash();
}
}
Now, you should be able to refactor it as OOP.
Current output
05/06/2020 : 669454683ee2e26f3ac1d18452437d58b8f07b780c9f898735881130cca69621