• 0

[Java] Arrays, HW, and more


Question

Hey everyone, its been quiite a while since I have posted at Neowin, however, I am in dire need of help with two beginning Java assignments. I have been behind in my class due to some lame personal reasons so im a bit lost. If anyone can help me with any of this it would be awesome :)

I apologize if this is viewed as "someone else doing my work," as I am merely only trying to better understand what needs to be done to complete the assignments.

The first assignement is to write a complete Java program in a source file to be named Assignment5.java. This file must hold class Assignment5 which has just one method: main.

The program simulates a compass. The program prompts the user for the following menu:

1. Turn North

2. Turn South

3. Turn West

4. Turn East

5. Quit

and based on the user choice prints ^ , V, < , or > respectively. Your program should continue with the menu until the user chooses to quit (choice number 5). You are required to use at least one switch statement in this program.

Sample Output: The user input is in brackets[ ]

1. Turn North

2. Turn South

3. Turn West

4. Turn East

5. Quit

Please select a command:[1]

^

1. Turn North

2. Turn South

3. Turn West

4. Turn East

5. Quit

Please select a command:[3]

<

1. Turn North

2. Turn South

3. Turn West

4. Turn East

5. Quit

Please select a command:[5]

Link to comment
https://www.neowin.net/forum/topic/597885-java-arrays-hw-and-more/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

And now for the part where I have nooo clue where to start. :(

This assignment is to create a class called NumberCollection in a file called NumberCollection.java. (there is no main method in this class). A class NumberCollection has an array of integers and a count (integer) as instance variables. The variable count keeps track how many integers are store in the array. The variable name for the array of integers is numberArray.

Note: You need to distinguish the array size (capacity) and "count" that keeps track of numbers added to this array so far.

The class NumberCollection must include the following constructor and methods. (If your class does not contain any of the following methods, points will be deducted.)

Method

Description of the Method

public NumberCollection(int arraySize)

It constructs an empty NumberCollection object with an array capacity specified by the integer parameter "arraySize".

private int indexOf(int searchingNum)

It returns the index of the number specified by the parameter is located. If the number is not found, it returns -1. It is a service (helper) method.

public boolean addNumber(int numberToAdd)

The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and also checks if the array has not reached its capacity. If both are satisfied, the number is added to the array at the smallest available index. If the array reached its capacity, double its size by calling the method doubleArrayCapacity() and add the number. If the number is added successfully, then the method returns true. If the number already exists in the array, the new number will not be added, and the method returns false.

public boolean removeNumber(int numberToRemove)

The method checks if the integer specified by the parameter exists in the array (This can be done using the indexOf method to see if it returns -1 or not) and if it does, it moves the index stored in the last index (count-1) to where numberToRemove was found, and changes the content at the last index to 0 and return true. Otherwise, it returns false.

private void doubleArrayCapacity()

It is a service (helper) method and doubles the capacity of the numberArray. Please see the example in page 338, the method increaseSize() as a reference.

public int findMax()

It finds the maximum number among the numbers stored so far (at the time when this method is called), and returns it. If the array is empty, return 0.

public int findMin()

It finds the minimum number among the numbers stored so far (at the time when this method is called), and returns it. If the array is empty, return 0.

public int computeSum()

It computes and returns the sum of numbers stored in the numberArray so far (at the time when this method is called.) If the array is empty, return 0.

public String toString( )

Returns a String containing a list of numbers stored in the numberArray. An example of such string can be:

(3, 6, -1, 3, 23, -50, 43)

The string should start with a '(' and end with a ')'.

Save the NumberCollection class in a file called NumberCollection.java and use the following program stored in Assignment6.java, which has the main method to create new NumberCollection objects and to test your class.

The program will ask a user to enter a size for the array. Then it will show the following menu to a user:

Command Options

-----------------------------------

a: add an integer in the array

b: remove an integer from the array

c: display the array

d: compute and display the maximum

e: compute and display the minimum

f: compute and display the sum

?: display the menu again

Then it will ask a user to enter one of the above commands. Based on the user's choice, the program needs to perform corresponding operation. This will be done by using a method you defined in the NumberCollection class. The program will terminate when a user enters 'q'.

Any help, recommendations, or input is welcomed for both of these assignments.

Thanks everyone :)

  • 0

Start by creating the classes and by adding the required methods first.

(the methods dont have to do anything, just so you can get a picture of what your class looks like, better yet draw yourself some diagrams of the classes)

create a driver class that has a main, that will instigate/run your code.

you should post some code or attempt something before asking for help here.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Yay, a forced new tab telling everyone edge just updated with a load of advertising in it twice as often as we currently get it. That'll never be abused. 😏
    • About bloody time. I have got PRs with hundred of files and the Web UI just struggling to even load the pages.
    • I wonder if it was applying secure boot certificates/dbx files?
    • I recently tried edge. It seems a lot better. A lot of the junk in it is gone. It seems less bloated and snappy.
    • Lethal fake phone chargers are still being sold on Amazon and eBay, UK watchdog warns by Paul Hill Credit: Pexels The UK consumer rights organization, Which?, is claiming that “potentially lethal knock-off chargers” are still being sold on online marketplaces seven years after it exposed the danger of these chargers. In its latest investigation, it bought 15 USB phone chargers from several online marketplaces and found they were missing key information, meaning they cannot be legally sold in the UK. Which? bought the 15 chargers from seven online marketplaces. These were Amazon (including Amazon Haul), AliExpress, B&Q Marketplace, Debenhams Marketplace, and eBay. It said that the chargers were so badly made that anyone using them was at risk of electric shock. Over half the chargers also posed fire and explosion risks. Of the chargers purchased, one was a fake Apple USB-C 35W power adaptor charger. To confuse buyers, the box was branded with an Apple logo, but testing found it to be a fake. Further testing picked up arcing sounds after 10 seconds of use, where a current jumps between two parts of the electrical circuit, which can cause fires, explosions, or electric shock. The manufacturers of this particular charger also put modeling clay inside it to make it feel more weighty, robust, and genuine. Not all of the chargers were technically faulty; however, some were missing key packaging, markings, and documentation, meaning they can’t be sold in the UK legally. Which? said that it is now campaigning alongside a coalition of safety groups and businesses for new laws that make online marketplaces responsible for ensuring the safety of products that they choose to list on their websites. It also said the government needs to start using powers under the Product Regulation and Metrology Act, which was adopted last July, to impose safety requirements on online marketplaces via secondary legislation, but so far, there have been delays. No matter what country you are in, be sure to properly research what you are buying and only buy authentic chargers to prevent fires. You can read more about Which?’s research here.
  • Recent Achievements

    • Week One Done
      Timaximus earned a badge
      Week One Done
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
    • Week One Done
      davidbazooked earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      499
    2. 2
      PsYcHoKiLLa
      174
    3. 3
      +Edouard
      160
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!