Question

In: Computer Science

8.37 Lab 8b: Count by 3 Expanded Task This program is a continuation of Lab 8a....

8.37 Lab 8b: Count by 3 Expanded

Task

This program is a continuation of Lab 8a. Create a program called CountBy3.java that will accept two numbers from the user, one to serve as the starting point and the other number as the ending point.

For this program, you will need two loops. The first loop will handle the print out "Counting by 3 from [starting value] to [ending value]". The ending value may not be what is inputted from the user. Instead, your loop will determine if you have to modify the ending value to be a number that is some multiple of 3 times the starting value. If there is any confusion, check out the sample output below.

Your second loop will then count by 3 up until the ending value or before if the ending value is not divisible by 3. Print the numbers separated by a space.

Sample output for input of 2 and 31:

Enter starting value: 2
Enter ending value: 31
Counting by 3 from 2 to 29:
2 5 8 11 14 17 20 23 26 29

IMPORTANT: Pieces of this code will be very similar to lab 8a and the two loops will be similar as well.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

import java.util.*;

/*class definition*/

public class CountBy3

{

    /*main method*/

    public static void main(String[] args)

    {

        /*Scanner class to read input from the user*/

        Scanner read=new Scanner(System.in);

        /*variables*/

        int start,end,i,dend=0;

        /*read start value*/

        System.out.print("Enter starting value: ");

        start=read.nextInt();

        /*read end value*/

        System.out.print("Enter Ending value: ");

        end=read.nextInt();

        /*find the ending value*/

        for(i=start;i<end;i+=3)

            dend=i;

        /*print header*/

        System.out.println("Counting by 3 from "+start+" to "+dend+":");

        /*print numbers separated by space*/

        for(i=start;i<end;i+=3)

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

    }

}




Related Solutions

8a. Describe the proposed mechanims of the lactose permease symporter. 8b. What are the differences of...
8a. Describe the proposed mechanims of the lactose permease symporter. 8b. What are the differences of hydrophobic pockets in chymotrypsin, trypsin and elastase?
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter...
---- Python CIMP 8A Code Lab 4 Write a program that asks the user to enter 5 test scores. The program will display a letter grade for each test score and an average grade for the test scores entered. The program will write the student name and average test score to a text file (“studentgrades.txt”). Three functions are needed for this program. def letter_grade( test_score) Test Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F...
8a). What general characteristics are found in many transposable elements? 8b). How does a retrotransposon move?...
8a). What general characteristics are found in many transposable elements? 8b). How does a retrotransposon move? 8c). Research has shown that more mutations accumulate in regions of the chromosome that consist of compact chromatin, such as heterochromatin. Offer an explanation for why mutation rates would be higher when chromatin is more compact.
Due Sunday Course Objectives 7a, 7b, 7c, 8a, 8b Homework Meaning: We have spent much of...
Due Sunday Course Objectives 7a, 7b, 7c, 8a, 8b Homework Meaning: We have spent much of the class looking at the advantages of the market system: this was essentially the section on perfect competition as well as supply and demand. Over the next 2 weeks, we will look at some of the disadvantages of the market system. This week, we look at what happens when markets are less than competitive. Fill in the following table for a monopoly. Q P...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that...
e. Task 5 (3 pts): (This task uses Strings and an if..then..elseif cascade) A program that prompts the user for their party affiliation (Democrat, Republican, or Independent) and responds accordingly with a Donkey,
Lab-3C   Pre-test Loop Write a MIPS program to count the number of students who failed in...
Lab-3C   Pre-test Loop Write a MIPS program to count the number of students who failed in a course. The final grades are given in an array. The fail grade is 60. Example: Array and Output      .data NoOfStudents: 10 Values:   60 70 80 50 90 80 55 90 80 70 Output: 2 students failed      .data NoOfStudents: 12 Values:   60 70 80 50 90 80 55 90 80 70 55 80 Output: 3 students failed
*********C++ Program************ This assignment is the continuation of project 4, with the modification and improvement with...
*********C++ Program************ This assignment is the continuation of project 4, with the modification and improvement with the following modifications: In Frac.h, replace the methods addition (const Fraction & ), subtraction( const Fraction& ), multiply( const Fraction & ), divide( const Fraction & ), void printFraction() by overloading operators +, -, *, /, << respectively. Add overloading operators > and < so that they can compare two Fractions. Write your own implementation file which is named Frac2.cpp to replace Frac.cpp and...
Write a Python program to count occurrences of items (and retrieve the most 3 or least...
Write a Python program to count occurrences of items (and retrieve the most 3 or least 3 words). Write a Python program to sort a dictionary by keys or values in ascending or descending order by 2 methods.
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. (Must use the code below without changing algorithms) For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT