Question

FRQ: TriangleNumberTracker

This question involves the implementation of the TriangleNumberTracker class which is used to keep track of triangle numbers. The triangle numbers form a sequence, the first 5 terms of which are shown below. This first term of this sequence is 1, and subsequent terms are found by incrementing the previous term by the term number.

Term number12345
Triangle Number1361015
Explanation
1 + 2 = 33 + 3 = 66 + 4 = 1010 + 5 = 15

TriangleNumberTracker object is created with an integer term number which determines the starting term to which the tracker refers. For example if the term number is 4, the tracker begins at the 4th triangle number, which is 10.

The TriangleNumberTracker class provides a constructor and the following methods.

  • currentTriangle which returns the triangle number which the tracker points at as an int.

  • nextTriangle which changes the tracker to point at the next triangle number.

  • prevTriangle which changes the tracker to point at the previous triangle number, unless it already points at the first triangle number (1).

The following table contains a sample code execution sequence and the corresponding results.

Statements and ExpressionsValue Returned (blank if no value)Comment
TriangleNumberTracker t = new
TriangleNumberTracker(4);

t is initialized pointing to the 4th triangle number
t.currentTriangle();10

The 4th Triangle number is 10

t.nextTriangle();
Moves current triangle number to the 5th
t.currentTriangle();15The 5th Triangle number is 15
t.nextTriangle();
Moves current triangle number to the 6th
t.currentTriangle();21The 6th Triangle number is 21
t.prevTriangle();
Moves current triangle number to the 5th
t.prevTriangle();
Moves current triangle number to the 4th
t.prevTriangle();
Moves current triangle number to the 3rd
t.currentTriangle();6The 3rd Triangle number is 6
t.prevTriangle();
Moves current triangle number to the 2nd
t.prevTriangle();
Moves current triangle number to the 1st
t.currentTriangle();1The 1st Triangle number is 1
t.prevTriangle();
Does nothing as current triangle number
is already 1st (lowest possible)
t.currentTriangle();1The 1st Triangle number is 1

Write the complete TriangleNumberTracker class including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

To view a PDF version of this prompt, click here (Links to an external site.).

If you get stuck, you can reference the scoring rubric (Links to an external site.) to help you debug your code.

Please note: The code for these FRQ activities has no main method, so it will not run in the Run Code area of your programming environment. If you would like to run your code, you will need to add a main method to your program. To score your code, you cannot have a main method in your program.


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

// defining class TriangleNumberTracker

public class TriangleNumberTracker

{

    // 2 instance variables to keep record of term number and triangle number

    int termNumber;

    int triangleNumber;

    

    // constructor of class

    public TriangleNumberTracker(int n)

    {

        termNumber = n; // initializing termNumber with provided n

        triangleNumber = 0; // initialize triangleNumber with 0

        // adding terms to calculate triangleNumber

        for(int i=1; i<=termNumber; i++)

        {

            triangleNumber += i;

        }

    }

    // method currentTriangle which returns the triangle number which the tracker points

    public int currentTriangle()

    {

        return triangleNumber;

    }

    // nextTriangle which changes the tracker to point at the next triangle number

    public void nextTriangle()

    {

        termNumber += 1;

        triangleNumber += termNumber;

    }

    

    // prevTriangle which changes the tracker to point at the previous triangle number

    public void prevTriangle()

    {

        if(termNumber > 1){

            triangleNumber -= termNumber;

            termNumber -= 1;

        }

    }

    

    // defining main with provided sequence of code

    public static void main(String[] args)

    {

        TriangleNumberTracker t = new TriangleNumberTracker(4);

        System.out.println(t.currentTriangle());

        t.nextTriangle();

        System.out.println(t.currentTriangle());

        t.nextTriangle();

        System.out.println(t.currentTriangle());

        t.prevTriangle();

        t.prevTriangle();

        t.prevTriangle();

        System.out.println(t.currentTriangle());

        t.prevTriangle();

        t.prevTriangle();

        System.out.println(t.currentTriangle());

        t.prevTriangle();

        System.out.println(t.currentTriangle());

    }

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
FRQ: TriangleNumberTracker
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
  • Hi I have 4 problems below I need done ASAP, if you could upload them one...

    Hi I have 4 problems below I need done ASAP, if you could upload them one by one by order you finish them instead of all at once that would be appreciated, I need this done in basic c++ code, all of the body structure are below I just need codes to be implemented for the codes to give the write outputs, Thank you. Q1. Update the given code 1 to implement the following problem: Create a class animal Set...

  • Hame manual transmission: 1st-3.24, 2nd - 2.10, 3rd - 1.42, 4th - 1.00, 5th - 0.81...

    Hame manual transmission: 1st-3.24, 2nd - 2.10, 3rd - 1.42, 4th - 1.00, 5th - 0.81 and 6th - 0.62. The differential has a ratio of 3.55:1 (the tire spins slower than the output of the transmission). The rear tire radius is 13.63 inches and the engine's maximum speed is 7500rpm. If the car is travelling at 105mph, what is the lowest selectable gear by number and corresponding engine speed? A 2018 Mustang GT has the following gear ratios in...

  • Sweaters sell for $20 at the crafts fair. Allie knits sweaters and her marginal costs are...

    Sweaters sell for $20 at the crafts fair. Allie knits sweaters and her marginal costs are given in the table below. Allie’s marginal costs increase with each additional sweater. If Allie is behaving rationally, how many sweaters will she sell? Marginal Cost ($) 1st sweater 3 2nd sweater 6 3rd sweater 10 4th sweater 16 5th sweater 23 6th sweater 30

  • Write a program that inputs a number between 1 and 10 and displays the number with...

    Write a program that inputs a number between 1 and 10 and displays the number with the appropriate two-letter ending (e.g., 1st, 2nd, 3rd, 4th, 5th, . . . ). Using Visual Logic

  • Suppose that Jacob spends his entire (I) of $100 on concerts (C) and movies (M). The...

    Suppose that Jacob spends his entire (I) of $100 on concerts (C) and movies (M). The prices of a concert (Pc) and movie (Pm) are $20 and $10, respectively. The following table summarizes the data on his marginal utity per dollar (MU/P) for each unit of concerts and movies. ((Concerts (C) (Pc = $20) ((Movies (M) (Pm = $10) C MUc MUc/Pc M MUm MUm/Pm 1st 100 1st 60 2nd 80 2nd 45 3rd 60 3rd 40 4th 40 4th...

  • Rank the following electronic transitions from most energetic to least. I. 4th n 3 ->n 2...

    Rank the following electronic transitions from most energetic to least. I. 4th n 3 ->n 2 II. 6th(least energetic) n 4-n = 1 II.1st (most energetic) n 2-n 1 n 8 n 3 IV. 3rd n 6->n 3 V. 2nd n 7 ->n 2 VI. 5th

  • 2. (6 points) (a) (3 points) The following recursively defined sequence is similar to the Fibonacci...

    2. (6 points) (a) (3 points) The following recursively defined sequence is similar to the Fibonacci Sequence: a, = 0, Q2 = as = 1, and an+1 = an - 3an-1 + An-2 for n > 3. Calculate the 4th, 5th, and 6th terms of this sequence. (b) (3 points) Evaluate S= lim n+0 (2n? - 12n" + 161n 3n4 - 162n +1 Be careful to justify your answer by showing the rules of limits and other results that you...

  • **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of...

    **Program must compile under Ubuntu! (35 pts) Write a C++ program A4p2.cpp with a class of your own design. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called playthat should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var...

  • Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John...

    Hi, I need help in solving Python programming problem below: Thank you. Best Regards When John gambles at the casino, he always uses a special system of tactics that he devised himself. It's based on always betting in one of two ways in each game: • betting exactly one chip (to test his luck); • betting all-in (he bets everything he has). Wins in the casino are paid equal to the wager, so if he bets C chips and wins,...

  • Element J was found to have the following ionization energies (in kJ/mol) 1st IE 2nd IE...

    Element J was found to have the following ionization energies (in kJ/mol) 1st IE 2nd IE 3rd IE 4th IE 5th IE 6th IE 631 1235 2389 7099 8844 10720 How many valence electrons does element J have? Explain. (2) If element J is found in the fourth period, identify element J. (1) Write the electronic configuration for element J. (1) Write complete sets of quantum numbers for each of the valence electrons (i.e. n, l, and ml) that you...

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