Here's my code by i don't know how can i use inheritance to my code can you help me to correct it.Post what you have done so far. Don't expect that someone will make that code for your here. Give us your solution first, and then we will provide you with the necessary help that you need based on your output.
Post your code properly by using the code tag. Here's the option.View attachment 1884434
Di kasi convenient basahin code pag puro left-aligned..![]()
import java.util.*;
class OutOfRangeException extends Exception{
public OutOfRangeException(){
super("Out of range\nGuess a number from 1 to 50!");
}
}
public class main1{
static void game(int guess, int target){}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Random r = new Random();
int target = r.nextInt((50 - 1) + 1) + 1;
int guess = -1;
System.out.println("Guess a number from 1 to 50!");
int count=1;
do{
try{
guess = sc.nextInt();
if(guess<1 || guess>50)
throw new OutOfRangeException();
if(guess<target){
System.out.println("Too low. Try again.");
count+=1;
}
if(guess>target){
System.out.println("Too high. Try again.");
count+=1;
}
}
catch(OutOfRangeException e){
System.out.println(e.getMessage());
count+=1;
}
catch(InputMismatchException e){
System.out.println("Invalid Input\nGuess a number from 1 to 50!");
count+=1;
sc.nextLine();
}
}while(guess!=target);
}
}
Code:import java.util.*; class OutOfRangeException extends Exception{ public OutOfRangeException(){ super("Out of range\nGuess a number from 1 to 50!"); } } public class main1{ static void game(int guess, int target){} public static void main(String args[]){ Scanner sc = new Scanner(System.in); Random r = new Random(); int target = r.nextInt((50 - 1) + 1) + 1; int guess = -1; System.out.println("Guess a number from 1 to 50!"); int count=1; do{ try{ guess = sc.nextInt(); if(guess<1 || guess>50) throw new OutOfRangeException(); if(guess<target){ System.out.println("Too low. Try again."); count+=1; } if(guess>target){ System.out.println("Too high. Try again."); count+=1; } } catch(OutOfRangeException e){ System.out.println(e.getMessage()); count+=1; } catch(InputMismatchException e){ System.out.println("Invalid Input\nGuess a number from 1 to 50!"); count+=1; sc.nextLine(); } }while(guess!=target); } } [/CO [/QUOTE]
Pacorrect po paano po sya maging inheritance cp lang po kasi gamit ko po at sa jdoodle po ako nag proprogram salamat po sa makakatulongCode:import java.util.*; class OutOfRangeException extends Exception{ public OutOfRangeException(){ super("Out of range\nGuess a number from 1 to 50!"); } } public class main1{ static void game(int guess, int target){} public static void main(String args[]){ Scanner sc = new Scanner(System.in); Random r = new Random(); int target = r.nextInt((50 - 1) + 1) + 1; int guess = -1; System.out.println("Guess a number from 1 to 50!"); int count=1; do{ try{ guess = sc.nextInt(); if(guess<1 || guess>50) throw new OutOfRangeException(); if(guess<target){ System.out.println("Too low. Try again."); count+=1; } if(guess>target){ System.out.println("Too high. Try again."); count+=1; } } catch(OutOfRangeException e){ System.out.println(e.getMessage()); count+=1; } catch(InputMismatchException e){ System.out.println("Invalid Input\nGuess a number from 1 to 50!"); count+=1; sc.nextLine(); } }while(guess!=target); } }
Sabi kasi ng teacher namin gumamit ng inheritance for this assignments kaso inapply ko na lahat ng napanood ko kaso ayaw nya nag eerror pede pakicorrect po itong code ko po for inheritance kasi jdoodle lang po pinagagawa yan thanks in advance bossAno concern mo sa code? ex: may error ba or what?
import java.util.Random;
import java.util.Scanner;
public class LabExer5A{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
// generating random number
int generatedNumber = random.nextInt(49) + 1;
// to store the count
int count = 1;
System.out.println("Guess a number from 1 to 50!");
while (true) {
try {
// user input
int num = sc.nextInt();
// if number is out of range then throw exception
if (num < 1 || num > 50) {
System.out.println("Out of range");
break;
}
// comparing the random number with user's input
if (num == generatedNumber) {
System.out.println("You got it in " + count + " attempt(s)!");
break;
} else if (num > generatedNumber) {
System.out.println("Too high. Try again.");
} else {
System.out.println("Too low. Try again.");
}
// incrementing the count
count += 1;
}
// catch exception and print the message
catch (Exception e) {
System.out.println("Invalid Input");
break;
}
}
}
}
Nageerror sya boss pagginagawa ko ng inheritance pacorrect nalang ng code ko salamat.Sabi kasi ng teacher namin gumamit ng inheritance for this assignments kaso inapply ko na lahat ng napanood ko kaso ayaw nya nag eerror pede pakicorrect po itong code ko po for inheritance kasi jdoodle lang po pinagagawa yan thanks in advance boss
Code:import java.util.Random; import java.util.Scanner; public class LabExer5A{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random random = new Random(); // generating random number int generatedNumber = random.nextInt(49) + 1; // to store the count int count = 1; System.out.println("Guess a number from 1 to 50!"); while (true) { try { // user input int num = sc.nextInt(); // if number is out of range then throw exception if (num < 1 || num > 50) { System.out.println("Out of range"); break; } // comparing the random number with user's input if (num == generatedNumber) { System.out.println("You got it in " + count + " attempt(s)!"); break; } else if (num > generatedNumber) { System.out.println("Too high. Try again."); } else { System.out.println("Too low. Try again."); } // incrementing the count count += 1; } // catch exception and print the message catch (Exception e) { System.out.println("Invalid Input"); break; } } } }
if(guess<0 || guess>50)
throw new NumberOutOfRangeException("Number out of range. " +
"Number must be in 1..50 inclusive.");
class NumberOutOfRangeException extends Exception
{
public NumberOutOfRangeException(String msg) {
super(msg);
}
}
Boss eto na tama ko na yung codesYou mean yung error nagnyayari sa exception?
try this:
Code:if(guess<0 || guess>50) throw new NumberOutOfRangeException("Number out of range. " + "Number must be in 1..50 inclusive.");
This is the class
Code:class NumberOutOfRangeException extends Exception { public NumberOutOfRangeException(String msg) { super(msg); } }
import java.util.Random;
import java.util.Scanner;
public class LabExer5A{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
// generating random number
int generatedNumber = random.nextInt(49) + 1;
// to store the count
int count = 1;
System.out.println("Guess a number from 1 to 50!");
while (true) {
try {
// user input
int num = sc.nextInt();
// if number is out of range then throw exception
if (num < 1 || num > 50) {
System.out.println("Out of range");
break;
}
// comparing the random number with user's input
if (num == generatedNumber) {
System.out.println("You got it in " + count + " attempt(s)!");
break;
} else if (num > generatedNumber) {
System.out.println("Too high. Try again.");
} else {
System.out.println("Too low. Try again.");
}
// incrementing the count
count += 1;
}
// catch exception and print the message
catch (Exception e) {
System.out.println("Invalid Input");
break;
}
}
}
}
class RandomNumber{
private int intNumber=0;
public RandomNumber(){
Random random= new Random();
this.intNumber = random.nextInt((50 - 1) + 1) + 1;
}
public int getRandom(){
return this.intNumber;
}
}
RandomNumber rand = new RandomNumber();
int rNum = rand.getRandom();