Can you help me if this my program use method or class. Because my teacher what us to use method or class, not all program are in main method. Can you correct it if i dont use the main or class. I am firstimer in programming. Thanks.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Can you help me if this my program use method or class. Because my teacher what us to use method or class, not all program are in main method. Can you correct it if i dont use the main or class. I am firstimer in programming. Thanks.

47
System.out-printIn("Out of range.");
System.out.println("Guess a number from 1 to 50: ");
}
}
48
49
50
51
System.out.println("You got it in " + count_guess + " attempts (s)!");
}
52
53
54
55
public static int guessNumber (int min, int max) {
int guess;
56 -
57
if (scan.hasNextInt()) {
guess = scan.nextInt();
} else {
scan.next();
throw new InputMismatchException();
}
if (guess<min || guess>max) {
throw new ArithmeticException();
}
58
59
60
61
62
63
64
65
66
67
68
return guess;
69
70 }
71
v Execute Mode, Version, Inputs & Arguments
CommandLine Arguments
JDK 17.0.1
Interactive
Execute
...
Result
Transcribed Image Text:47 System.out-printIn("Out of range."); System.out.println("Guess a number from 1 to 50: "); } } 48 49 50 51 System.out.println("You got it in " + count_guess + " attempts (s)!"); } 52 53 54 55 public static int guessNumber (int min, int max) { int guess; 56 - 57 if (scan.hasNextInt()) { guess = scan.nextInt(); } else { scan.next(); throw new InputMismatchException(); } if (guess<min || guess>max) { throw new ArithmeticException(); } 58 59 60 61 62 63 64 65 66 67 68 return guess; 69 70 } 71 v Execute Mode, Version, Inputs & Arguments CommandLine Arguments JDK 17.0.1 Interactive Execute ... Result
import java.security.GeneralSecurityException;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
1
2
4.
5
6- public class bar6 {
public static Random rand = new Random();
public static Scanner scan = new Scanner(System.in);
7
public static void main(String[] args) {
int min = 0;
int max = 50;
10
11
12
13
14
int rand_num = rand.nextInt (max-min+1)+min;
15
16
System.out.println("Guess a number from 1 to 50: ");
int guess;
int count_guess = 0;
17
18
19
20
while (true) {
try {
21
22
23
24
guess = guessNumber (min, max);
25
26
count_guess += 1;
27
if (guess > rand_num) {
System.out.println("Too high. Try again.");
}
28
29
30
31
32 -
else if (guess < rand_num) {
System.out.println("Too low. Try again.");
}
33
34
35
else {
break;
}
36
37
38
39
40
} catch(InputMismatchException e) {
41
System.out.println("Invalid input.");
System.out.println("Guess a number from 1 to 50: ");
42
43
44
45
} catch (ArithmeticException e) {
46
47
System.out.println("Out of range.");
Transcribed Image Text:import java.security.GeneralSecurityException; import java.util.InputMismatchException; import java.util.Random; import java.util.Scanner; 1 2 4. 5 6- public class bar6 { public static Random rand = new Random(); public static Scanner scan = new Scanner(System.in); 7 public static void main(String[] args) { int min = 0; int max = 50; 10 11 12 13 14 int rand_num = rand.nextInt (max-min+1)+min; 15 16 System.out.println("Guess a number from 1 to 50: "); int guess; int count_guess = 0; 17 18 19 20 while (true) { try { 21 22 23 24 guess = guessNumber (min, max); 25 26 count_guess += 1; 27 if (guess > rand_num) { System.out.println("Too high. Try again."); } 28 29 30 31 32 - else if (guess < rand_num) { System.out.println("Too low. Try again."); } 33 34 35 else { break; } 36 37 38 39 40 } catch(InputMismatchException e) { 41 System.out.println("Invalid input."); System.out.println("Guess a number from 1 to 50: "); 42 43 44 45 } catch (ArithmeticException e) { 46 47 System.out.println("Out of range.");
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Function Calling
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education