Java language in jGRASP only please B01 - Automatic Learning Write a program in
ID: 3907184 • Letter: J
Question
Java language in jGRASP only please
B01 - Automatic Learning Write a program in a java file called ArtificialIntelligence.java. Context: You have a target random number between 0 and 10. Your program represents a model that generates random numbers between 0 and rangeMax trying to predict a number close to the target one. When the program predicts a random number between 0 and rangeMax, it will compute the absolute difference between the current output and the target number. If the difference is greater than 10, then your program will update its rangeMax variable to decrease it by 10. Do this until the difference is not greater than 10, in that case we consider the model is accurate enough. Your program will do the following: 1. Initialize a rangeMax variable to 100. 2. Call a function named generateGroundTruthData, that will compute and return a random number between 0 and 10. 3. Back in the main method, the value returned by the generateGroundTruthData method will be the target number. 4. Your program will continuously call the method train, which: 4.1. Computes a random number between 0 and the current rangeMax value. 4.2. Computes the absolute difference between the current predicted number and the target number. 4.3. Return the new value for the rangeMax. If the difference is greater than 10, then currentMax - 10. Otherwise, the new value is the same as the current value. 5. Back in the main method, update the rangeMax value with the new value returned by the train method and print this value. 6. If the value hasn't changed, then finish the program and print "The model has finished learning." B02 - Hashing a function Write a program in a java file called Hash.java. A hash function maps data of a variable length size to data of a fixed size. In this case, your program will first call a method askUserInput that will print the message "Please, enter your message:" and ask for the user input. This method will return the user input back to the main method. Then, your program will call a method processDigits that will iterate through all the characters in the user input message and return the sum of all the digits contained in the user message. This method will return the result back to the main method. Then, your program will call a method named processTextMessage. This method will return the number of characters in the text that are not number digits. Finally, your program will call a method printHash, that will receive the sum of all digits in the message and the count of characters that are not digits, and it will print the hash, which will be computed as the string concatenation of both numbers, in the previous order. B03 - Two-Step Authentication Write a program in a java file called Authentication.java. Your program will call two methods, one that asks the user for the username and returns this value, and another method to ask the user for the password and returns this value. Then, your program will call the method validateCredentials that will check if the username equals "myusername" and password equals "pass123". If the result is false, your program will print "Invalid username or password". Otherwise, your method will print "Log in successful". This method will return whether the login was successful or not. Back in your main method, your program will keep asking the user for the credentials and checking with the expected ones until the user enters the correct username and password. B04 - Academic admission system Write a program in a java file called Admissions.java You will write a program that will either accept or deny graduate admissions based on the grades obtained in the undergraduate program. The conversion from letter grades to GPA points is the following: LETTER GRADE GPA points A+ 4.3 A 4.0 A- 3.7 B+ 3.3 B 3.0 B- 2.7 C+ 2.3 C 2.0 C- 1.7 D+ 1.3 D 1.0 D- 0.7 F 0.0 Your program will call a method askUserGrade, that asks the user for the letter grade obtained in an undergraduate course and returns the corresponding GPA points. In order for the user to finish entering grades, the user will enter "EXIT" and your program will return -1. Back in the main method, your program will accumulate the GPA points and count the number of grades entered by the user. When the user enters EXIT, then your program stop asking for more grades. When the program stops asking for grades, it will call a method named computeAverageGPA that, based on the accumulated points and the number of grades entered, will compute and return the average GPA points. Then, your program will call a method named askForProgram, where the user will enter either "masters" or "phd", but in any combination of lowercase and uppercase letter. Your method will return the user input. Back in the main method, your program will call the processApplication, where the program will decide whether or not the application is accepted depending on the average GPA, the program the user is applying to and the following rules. Your method will print the final decision: - If the user is applying to the Masters program, then he/she needs a GPA above 3.0 - If the user is applying to the PhD program, then he/she needs a GPA above 3.5 NOTE: The user can enter any combination of uppercase and lowercase letters for "masters" or "phd". You can assume users will always enter any of these two values. That is, the user can enter "Masters", "MASTERS", "masters", "MaStErS", "PHD", "phd", "PhD", etc. but not "doctorate", "bachelors", etc.
Explanation / Answer
Here is the completed code for questions B01, B02 and B03. Cannot complete B04 as there is not enough time. Please split the parts and post each of them as different questions whenever you are posting like this. Or atleast post two parts in each question. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// ArtificialIntelligence.java (B01)
import java.util.Random;
public class ArtificialIntelligence {
static int rangeMax, target;
static Random random = new Random();
/**
* method to generate a random number between 0 and 10
*/
public static int generateGroundTruthData() {
return random.nextInt(11);
}
/**
* method to generate a random number between 0 and rangeMax, find the
* difference between that number and target, return rangeMax-10 if the
* difference is greater than 10, else rangeMax
*/
public static int train() {
int num = random.nextInt(rangeMax);
//finding the absolute difference
int difference = Math.abs(num - target);
if (difference > 10) {
//difference greater than 10
return rangeMax - 10;
} else {
return rangeMax;
}
}
public static void main(String[] args) {
rangeMax = 100;
//generating target number
target = generateGroundTruthData();
//calling train for first time
int rangeMaxNew = train();
/**
* looping until the new and old values are same
*/
while (rangeMax != rangeMaxNew) {
rangeMax = rangeMaxNew;
//displaying current value
System.out.println("Current rangeMax value: " + rangeMax);
rangeMaxNew = train();
}
System.out.println("The model has finished learning");
}
}
/*OUTPUT*/
Current rangeMax value: 90
Current rangeMax value: 80
Current rangeMax value: 70
Current rangeMax value: 60
Current rangeMax value: 50
Current rangeMax value: 40
Current rangeMax value: 30
Current rangeMax value: 20
The model has finished learning
// Hash.java (B02)
import java.util.Scanner;
public class Hash {
static Scanner scanner = new Scanner(System.in);
/**
* method to prompt the user to enter a message, and return it
*/
public static String askUserInput() {
System.out.println("Please, enter your message: ");
String message = scanner.nextLine();
return message;
}
/**
* method to find the sum of all digits contained in the message
*/
public static int processDigits(String message) {
int sum = 0;
for (int i = 0; i < message.length(); i++) {
// checking if current character is a digit
if (Character.isDigit(message.charAt(i))) {
// converting to integer and appending to the sum
sum += Integer.parseInt(message.charAt(i) + "");
}
}
return sum;
}
/**
* method to find the count of all non- digit characters contained in the
* message
*/
public static int processTextMessage(String message) {
int count = 0;
for (int i = 0; i < message.length(); i++) {
// checking if current character is not a digit
if (!Character.isDigit(message.charAt(i))) {
// incrementing the count
count++;
}
}
return count;
}
/**
* method to print the hash which is a concatenated string of sumDigits and
* countNonDigits
*/
public static void printHash(int sumDigits, int countNonDigits) {
System.out.println("The hash is " + sumDigits + "" + countNonDigits);
}
public static void main(String[] args) {
//getting input
String message = askUserInput();
//finding digits sum
int sumDigits = processDigits(message);
//counting non digits
int countNonDigits = processTextMessage(message);
//printing hash
printHash(sumDigits, countNonDigits);
}
}
/*OUTPUT*/
Please, enter your message:
123helloWorld
The hash is 610
// Authentication.java (B03)
import java.util.Scanner;
public class Authentication {
static Scanner scanner = new Scanner(System.in);
/**
* method to prompt for username and return it
*/
public static String getUserName() {
System.out.println("Enter user name: ");
String str = scanner.nextLine();
return str;
}
/**
* method to prompt for password and return it
*/
public static String getPassword() {
System.out.println("Enter password: ");
String str = scanner.nextLine();
return str;
}
/**
* method to validate user name and password returns true if usr is
* "myusername" and pass is "pass123". otherwise false
*/
public static boolean validateCredentials(String usr, String pass) {
if (usr.equals("myusername") && pass.equals("pass123")) {
return true;
}
// username/password not valid
return false;
}
public static void main(String[] args) {
String usr = "", pass = "";
/**
* looping until validation is successful
*/
while (!validateCredentials(usr, pass)) {
/**
* getting username and password
*/
usr = getUserName();
pass = getPassword();
// validating
if (validateCredentials(usr, pass)) {
// success
System.out.println("Log in successful");
} else {
// failed
System.out.println("Invalid username or password");
}
}
}
}
/*OUTPUT*/
Enter user name:
hello
Enter password:
mypoass
Invalid username or password
Enter user name:
myusername
Enter password:
mypass
Invalid username or password
Enter user name:
myusername
Enter password:
pass123
Log in successful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.