Hi, I need help with a Java Program(we use Blue J) ?Write a class with a method
ID: 3620309 • Letter: H
Question
Hi, I need help with a Java Program(we use Blue J)?Write a class with a method that will request a number from the user that is between 5 and 15.
?A different method should write a grid from 1 to the number entered using a for loop and a different one using a while loop. Like the one bellow:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
I can’t get the grid right. Below is what I have please help me!!
/**
* will request a number from the user that is between 5 and 15 and a different method should write a grid from 1 to the number entered using a for loop and a different one using a while loop
*
* @author l)
* @version ()
*/
public class Number {
// The number the user entered.
private int num;
/**
* To set the number.
*/
public void setNumber(int numberNums) {
num = numberNums;
}
public void printDetails() {
/**
* Prints the number that was entered by the user.
* Checks to make sure it is between 5 and 15.
* If not it will ask the user to entered a number between 5 and 15.
*/
if (num >= 5 && num <= 15) {
System.out.println( num + " Is the number you entered.");
}
else {
System.out.println(" You must enter a number between 5 and 15.");
}
}
public class Number2Analyzer {
// Storage for nums.
private int[] num;
public Number2Analyzer() {
num = new int [10];
}
public void printDetails() {
int num = 0;
while(num <= 10) {
System.out.println(num);
num = num + 1;
}
}
}
}
Explanation / Answer
import java.util.Scanner;// Needed for the Scanner class
class Number
{
// The number the user entered.
private int num;
/**
* To set the number.
*/
public void setNumber(int numberNums)
{
num = numberNums;
}
public void printDetails()
{
/**
* Prints the number that was entered by the user.
* Checks to make sure it is between 5 and 15.
* If not it will ask the user to entered a number between 5 and 15.
*/
if (num >= 5 && num <= 15)
{
System.out.println( num + " Is the number you entered.");
}
else
{
String str;
System.out.println(" You must enter a number between 5 and 15.");
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number:");
num=keyboard.nextInt();
}
}
public void Grid()
{
//Loop reteats for each line
for(int i=1;i<=num;i++)
{
int j=i;
int count=0;
while(count<num)
{
System.out.print(j+" ");
j++;
count++;
}
System.out.println();
}
}
}
public class Testtool
{
public static void main(String args[])
{
String str;
int number;
//creating instance to class Number
Number obj=new Number();
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter number:");
number=keyboard.nextInt();
//function call to set number
obj.setNumber(number);
obj.printDetails();
//prints grid
obj.Grid();
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.