Hi, do you have a tutorial for writing java method with the following header that returns a pentagonal number: public static int pentagonal (int n). Then write a test program that uses this method to display the first 100 pentagonal numbers showing 10 numbers on each line, using 10 spaces for each number?
This makes sense but does not solve what is expected of me with my project currently. Teacher wants me to have 3 words and then generate random output of the 3 words
Instead of the full alphabet, you could set the list of characters to be the three words. This can be done dynamically from user input or hard-coded prior to execution.
Loved this tutorial! So simple and made it so clear. I decided to also implement a randomized number for the secretNumber and when guessed correctly gave an output of how many guesses it took you to get to the correct one. Enjoy! :) import java.util.Scanner; //to take in user input import java.util.Random; //to create a random number public class GuessGame { public static void main(String[] args) { int secretNumber; // what the number is set to int userGuess; // the user input int guessCount = 0; // number of users guesses boolean correct = false; // set to false so that it's changed to true when guessed correctly Scanner keyboard = new Scanner(System.in); // so that we can use the user input //Creates the max and min number the object can randomly generate int maxNumber = 1000; int minNumber = 1; //Creates a random number object Random randomNumber = new Random(); //Attaches the random number object to secretNumber making it a short number secretNumber = minNumber + randomNumber.nextInt(maxNumber); //Loop through asking for numbers until the user guesses the secretNumber. // Create loop to move through the guesses while(!correct) { // while not false == true System.out.print("Guess the number that I am thinking: "); userGuess = keyboard.nextInt(); //takes input from the user if(userGuess == secretNumber) { // if guessed correctly correct = true; System.out.println("!!!YOU'RE A WINNER!!!"); System.out.println("It took you " +guessCount+ " guesses!");// includes number of wrong guesses } else if (userGuess < secretNumber){ System.out.println("GUESS HIGHER"); guessCount++; //counts wrong guesses } else if (userGuess > secretNumber) { System.out.println("GUESS LOWER"); guessCount++;//counts wrong guesses } } } }
import java.util.Random; import java.util.Scanner; public class g { public static void main(String[] args) { Random random=new Random(); Scanner input=new Scanner(System.in); int Limit; int Guess; int rand; System.out.println("Enter The Limit of Guessing"); Limit=input.nextInt(); rand=random.nextInt(Limit); for (int i=0;i<=5;i++) { System.out.println("Enter The Number You Think Is Correct< 5Try Only >"); Guess=input.nextInt(); if (rand==Guess) { System.out.println("YOY ARE CORRECT!"); break; } else if (rand!=Guess) { System.out.println("SORRY TRY AGAIN"); } } } }
Good day Man. I really love your videos, just a quick question. Can a method be accessed from a subclass of an inherited class that is (This method should be different from the method of it's superclass)
One question that I have is why to use abstract methods when we already know that we are going to need to make a method in the instance and probably already knows what the method should do? I mean, for me, it makes more sense to make a non-static method speak() inside the Pet abstract class and make it generic for all the subclasses (name and age for parameters so when you call the speak method you just need to pass the name and age as arguments), and if you want to change the speak method inside a subclass, you just override in that subclass.
if u r writing this code in Notepad public class Overload { public void print(int num) { System.out.println("1st print method"); System.out.println("int: " + num); } public void print(double num) { System.out.println("2st print method"); System.out.println("double: " + num); } public void print(String text) { System.out.println("3st print method"); System.out.println("String: " + text); } public void print(int num1, int num2) { System.out.println("4st print method"); System.out.println("int: " + num1 + "int: " + num2); } public static void main(String[] args) { Overload object1 = new Overload(); object1.print (1); } } thank me later BTW thanks man for making it simple😁
Thank you for the explanation. I was feeling overwhelmed but I now have a better understanding. This has really helped me complete the exercises for my intro to Java class. Much appreciated.