jGRASP Java language B01 - Automatic Learning Write a program in a java file cal
ID: 3907267 • Letter: J
Question
jGRASP Java language
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:
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.
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.0Explanation / Answer
/**
* ArtificialIntelligence
*/
import java.lang.Math;
public class ArtificialIntelligence{
static int rangeMax=100;
static int generateGroundTruthData(){
int lower = 0;
int upper = 10;
int res = (int) (Math.random() * (upper - lower)) + lower;
return res;
}
static int train(int target)
{
int lower = 0;
int upper = ArtificialIntelligence.rangeMax;
int res = (int) (Math.random() * (upper - lower)) + lower;
int absdiff=res-target;
return absdiff;
}
public static void main(String[] args)
{
int target=generateGroundTruthData();
while(train(target)>10)
{
rangeMax-=10;
System.out.println("new rangeMax:"+rangeMax);
}
System.out.println("The model has finished learning.");
}
}
/**
* Hash
*/
import java.util.Scanner;
public class Hash{
static String askUserInput()
{ Scanner sc=new Scanner(System.in);
System.out.println( "Please, enter your message:" );
String msg=sc.nextLine();
sc.close();
return msg;
}
static int processDigits(String s)
{ int sum=0;
for (int i = 0; i < s.length(); i++){
if(Character.isDigit(s.charAt(i)))
sum +=Character.getNumericValue(s.charAt(i));
}
return sum;
}
static int processTextMessage(String s){
int count=0;
for (int i = 0; i < s.length(); i++){
if(!Character.isDigit(s.charAt(i)))
count +=1;
}
return count;
}
static void printHash(int sum,int count)
{
System.out.println(Integer.toString(sum)+""+Integer.toString(count));
}
public static void main(String[] args) {
String msg=askUserInput();
int sum=processDigits(msg);
int count=processTextMessage(msg);
printHash(sum, count);
}
}
/**
* Authentication
*/
import java.util.Scanner;
public class Authentication {
static String getusername()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter username:");
String username=sc.next();
return username;
}
static String getPassword()
{
Scanner sc = new Scanner(System.in);
System.out.println("enter password:");
String pass=sc.nextLine();
return pass;
}
static boolean validateCredentials(String username,String pass)
{
if(username.equals("myusername") && pass.equals("pass123"))
{
System.out.println("Log in successful");
return false;
}
else{
System.out.println("Invalid username or password");
return true;
}
}
public static void main(String[] args) {
boolean flag=true;
while(flag)
{
String username= getusername();
String pass=getPassword();
flag=validateCredentials(username, pass);
}
}
}
/**
* Admissions
*/
import java.util.Scanner;
public class Admissions {
static float askUserGrade(String grd)
{
switch (grd) {
case "A+":
return 4.3f;
case "A":
return 4.0f;
case "A-":
return 3.7f;
case "B+":
return 3.3f;
case "B":
return 3.0f;
case "B-":
return 2.7f;
case "C+":
return 2.3f;
case "C":
return 2.0f;
case "C-":
return 1.7f;
case "D+":
return 1.3f;
case "D":
return 1.0f;
case "D-":
return 0.7f;
case "F":
return 0.0f;
case "EXIT":
return -1f;
default:
return -99f;
}
}
static float computeAverageGPA(float sum,int num)
{
return (float)sum/num;
}
static String askForProgram()
{
System.out.println("enter program:");
Scanner sc = new Scanner(System.in);
String pgm=sc.next();
return pgm.toLowerCase();
}
static void processApplication(String pgm,float avg)
{
if (pgm.equals("masters") && avg==3.0f) {
System.out.println("Application Accepted.");
} else if (pgm.equals("phd") && avg==3.5f) {
System.out.println("Application Accepted.");
}
else{
System.out.println("Application Rejected.");
}
}
public static void main(String[] args) {
boolean flag=true;float sum=0;int num=0;
while(flag)
{
System.out.println("enter grade:");
Scanner sc = new Scanner(System.in);
String grd=sc.next();
if(askUserGrade(grd)==-1f)
flag=false;
else if(askUserGrade(grd)!=-99f)
{
sum+=askUserGrade(grd);
num+=1;
}
}
float avg=computeAverageGPA(sum, num);
String pgm=askForProgram();
processApplication(pgm, avg);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.