🔒 Closed Guys pahelp naman po

Status
Not open for further replies.

BATANG Singkit 01

Forum Veteran
Can anyone please help me with these? Using method or class. All program are not in the main class. Thank you in advance

image-cae2e68f-a883-4ce3-966e-b0d85e484051.webp
 
Can anyone please help me with these? Using method or class. All program are not in the main class. Thank you in advance

Guys pahingi codes naman dito using method or class. All program are not in the main class.

image-cae2e68f-a883-4ce3-966e-b0d85e484051.webp
 
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 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.
Here's my code by i don't know how can i use inheritance to my code can you help me to correct it.


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);

}

}
 
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.. :D
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);

    }

}

P
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]

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);

    }

}
Pacorrect po paano po sya maging inheritance cp lang po kasi gamit ko po at sa jdoodle po ako nag proprogram salamat po sa makakatulong
 
Ano concern mo sa code? ex: may error ba or what?
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;
   }
  }
 }

}

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;
   }
  }
 }

}
Nageerror sya boss pagginagawa ko ng inheritance pacorrect nalang ng code ko salamat.
 
You 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);
    }
}
 
You 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);
    }
}
Boss eto na tama ko na yung codes
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;
   }
  }
 }

}

Kaso paano ito magiging inheritance? Required kasi sa amin na wag daw lahat ng programs is nakalagay sa main class gumamit daw kami ng method or class pacorrect nalang boss buong codes ko dyan if paano. Salamat boss.
 
Gawa ka ang class to for random number and method to generate random number..Let the sub class perform the comparison..

1. Definition RandomNumber class
Code:
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;
    }
}

2. Implement the class
Code:
RandomNumber rand = new RandomNumber();      
int rNum = rand.getRandom();
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 10
    Replies
  • 1K
    Views
  • 2
    Participants
Last reply from:
vhin le

Trending Topics

Online now

Members online
1,311
Guests online
1,742
Total visitors
3,053

Forum statistics

Threads
2,272,076
Posts
28,940,049
Members
1,237,979
Latest member
02Emperor
Back
Top