Question

I do not know how to code this. I have tried several times.Instructions CountByAnything.java + Modify the CountByFives application so that the user enters the value to count by. Start

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi. Here is the completed code for this problem. Before that, I have to tell you, there is a lot of confusions in this question. It is not clear that the value we receive from the user is to be used as increment value or start value or both. It is not clear that the stop value is still 500. I’m assuming that the value we read from input is both the start value and increment value, for example if the input is 3, the system will print 3 6 9 12 .., if the input is 7, the system will print 7 14 21 … until 500, separating every 10 values by a newline, and each value in a line is separated by space. So if any of my assumption is wrong, your code may not pass the tests, but don’t panic, just let me know what results are you getting and I’ll update the code as needed.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

// CountByAnything.java

import java.util.Scanner;

public class CountByAnything {

      public static void main(String[] args) {

            // defining a Scanner to read input

            Scanner sc = new Scanner(System.in);

            // reading an integer, setting as START value.

            final int START = sc.nextInt();

            // using the same STOP value as before since not specified otherwise.

            final int STOP = 500;

            // using 10 as numbers per line

            final int NUMBER_PER_LINE = 10;

            // looping from START to STOP, incrementing by START at a time

            for (int i = START; i <= STOP; i += START) {

                  // displaying i followed by a space

                  System.out.print(i + " ");

                  // if i%(NUMBER_PER_LINE * START) is 0, it means we have printed

                  // NUMBER_PER_LINE values in this line, so printing a line break.

                  if (i % (NUMBER_PER_LINE * START) == 0) {

                        System.out.println();

                  }

            }

      }

}

Add a comment
Know the answer?
Add Answer to:
I do not know how to code this. I have tried several times. Instructions CountByAnything.java +...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that accepts a number of minutes and converts it both to hours and...

    Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. Grading Write your Java code in the area on the right. Use the Run button to compile and run the code. Clicking the Run Checks button will run pre- configured tests against your code to calculate a grade.

  • Modify the CountByFives application so that the user enters the value to count by. Start each...

    Modify the CountByFives application so that the user enters the value to count by. Start each new line after 10 values have been displayed. public class CountByAnything { // Modify the code below public static void main (String args[]) { final int START = 5; final int STOP = 500; final int NUMBER_PER_LINE = 50; for(int i = START; i <= STOP; i += START) { System.out.print(i + " "); if(i % NUMBER_PER_LINE == 0) System.out.println(); } } }

  • Bottom section is the what they expect in the code in java Create a class in...

    Bottom section is the what they expect in the code in java Create a class in JobApplicant.java that holds data about a job applicant. Include a name, a phone number, and four Boolean fields that represent whether the applicant is skilled in each of the following areas: word processing, spreadsheets, databases, and graphics. Include a constructor that accepts values for each of the fields. Also include a get method for each field. The get method should be the field name...

  • Create an abstract Student class for Parker University. The class contains fields for student ID number,...

    Create an abstract Student class for Parker University. The class contains fields for student ID number, last name, and annual tuition. Include a constructor that requires parameters for the ID number and name. Include get and set methods for each field; the setTuition() method is abstract. Create three Student subclasses named UndergraduateStudent, GraduateStudent, and StudentAtLarge, each with a unique setTuition() method. Tuition for an UndergraduateStudent is $4,000 per semester, tuition for a GraduateStudent is $6,000 per semester, and tuition for...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words)...

    Your code should contain appropriate validations and must focus on code optimization. Briefly, explain (150-200 words) the logic of the program and how the code can be   optimized. Modify the code to repeat the program until the user enters “no”. (*I need the logical write out on how the program behaves when there is an input from the user. (I don't need the //comments). import java.util.Scanner; public class Q2 {     public static void main(String[] args)     {         Scanner...

  • JAVA Modify the code to create a class called box, wherein you find the area. I...

    JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width;       public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...

  • 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...

  • Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...

    Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...

  • This is a java code but I need to understand the logic of how is going...

    This is a java code but I need to understand the logic of how is going to run. Can someone help please. int i = 1; while (i <= 5) { xMethod(i); i++; } System.out.println("i is " + i); } public static void xMethod(int i) { do { if (i % 2 != 0) System.out.print(i + " "); i--; } while (i >= 1); System.out.println();

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
Active Questions
ADVERTISEMENT