Question

For this assignment you are going to write code for the following ONE class called: Print_n_numbers...

For this assignment you are going to write code for the following ONE class called:

Print_n_numbers

Here are the specifications for the class: Make sure the class Print_n_numbers has a default constructor, i.e. it is possible to create an object of the class by executing:

new Print_n_numbers()

The class has only one method: print.

Specs for the method:

name: print arguments: one int, called n. returns: nothing what it does: it prints the numbers 1, 2, 3 ... n

if the number is 0 or negative the method print prints:

bad number

You can use the following OPTIONAL (i.e. whether you write it or not it will not affect the tests) main method:

public static void main(String[] aa){ Print_n_numbers ss = new Print_n_numbers(); Scanner scan = new Scanner(System.in); int ii = 0; while (ii != -1){ System.out.print("please enter a non-zero number (-1 to end): "); ii = scan.nextInt(); System.out.println("the following is the output of ss.print(" + ii + "):\n"); ss.print(ii); } }

If you have this main method, then you can type: Note: in the following example what the user would enter is shown in bold face. Otherwise it is the output of the program.

java Print_n_numbers please enter a non-zero number: 0 the following is the output of ss.print(0):

bad number

please enter a non-zero number: -2 the following is the output of ss.print(-2):

bad number

please enter a non-zero number: 1 the following is the output of ss.print(1):

1

please enter a non-zero number: 2 the following is the output of ss.print(2):

1, 2

please enter a non-zero number: 5 the following is the output of ss.print(5):

1, 2, 3, 4, 5

please enter a non-zero number: 10 the following is the output of ss.print(10):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

please enter a non-zero number: 30 the following is the output of ss.print(30):

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30

please enter a non-zero number: -1 the following is the output of ss.print(-1):

bad number

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
For this assignment you are going to write code for the following ONE class called: Print_n_numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • PrintArray vi Create a class called PrintArray. This is the class that contains the main method....

    PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...

  • 5. Write a static method "f(n)" in the space provide, that returns O if n is...

    5. Write a static method "f(n)" in the space provide, that returns O if n is even, and if n is odd, returns 1 or -1 according as n is greater than or less than 0. importjava.util.Scanner; public class Q_05 Your "f(n)" method: public static void main(String args[]) mana int n; Scanner input = new Scanner(System.in); System.out.print("Please enetrn: "); n=input.nextInt(); System.out.println(f(n)); "Method f(n)" 7. Write a static method "max" in the space provide, that returns the maximum value from 3...

  • The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...

    The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Problem #1 : write a Month class that contains methods to determine the number of days...

    Problem #1 : write a Month class that contains methods to determine the number of days in a month given the numeric equivalent of the month and year; your program must take into account leap years. To help facilitate a result, the following are sample runs and a driver program import java util.Scanner This is a test driver class for the Nonth class pablic class namDays ic statie void main (3teingt1 args Scanner in new Scanner (System.in) System.out print ("Please...

  • You will write a class called Shoe.java Shoe.java should have Three instance variables String brand (cannot...

    You will write a class called Shoe.java Shoe.java should have Three instance variables String brand (cannot be blank) double size (from 5 to 12) int color (a number from 1 to 5 representing one of 5 colors) This code is to control the amount of colors. the colors represented are as follows 1. red 2. green 3. blue 4. black 5. grey One constructor, one get method per instance variable, one set method per instance variable. You will need a...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT