Create and initialize an integer array

Assignment Help Computer Engineering
Reference no: EM132153436

Having some trouble with this Java Lab (Array.java)

Objective:

This lab is designed to create an array of variable length and insert unique numbers into it. The tasks in this lab include:

Create and Initialize an integer array

Create an add method to insert a unique number into the list

Use the break command to exit a loop

Create a toString method to display the elements of the array

Task 1:

Create a class called Array, which contains an integer array called ‘listArray'. The integer array should not be initialized to any specific size.

Task 2:

Create a Constructor with a single parameter, which specifies the number of elements that needs to be created for listArray. The listArray needs to be created to this size.

Task 3:

Create a ‘add' method to class Array which will search through the elements of listArray. A looping mechanism should be used to search the array of elements. If the value passed into add is not already in the list, it should be added. Once the element is added, you need to use the break statement to exit to search so the value isn't added to each space in the array. If all elements of the array contain non-zero numbers, no more values will be added.

Task 4:

Create a ‘toString' method to output the non-zero elements of the array. Use the program template and the Sample Output as a reference to guide your application development.

Sample output:

Enter Number of Elements to Create in Array: 10
Enter Number to Add to List (-1 to Exit): 4
4 (added)
Enter Number to Add to List (-1 to Exit): 9
9 (added)
Enter Number to Add to List (-1 to Exit): 21
21 (added)
Enter Number to Add to List (-1 to Exit): 4
4 (duplicate)
Enter Number to Add to List (-1 to Exit): 7
7 (added)
Enter Number to Add to List (-1 to Exit): -1
Array List: 4, 9, 21, 7,

Test Code (ArrayTest.java):

package arraytest; import java.util.Scanner; public class ArrayTest { public static void main(String[] args) { int elements, input; Scanner keyboard = new Scanner( System.in ); Array arrayList; System.out.print( "Enter Number of Elements to Create in Array: " ); elements = keyboard.nextInt(); // Read number of elements arrayList = new Array( elements ); // Instantiate array with no elements do { System.out.print( "Enter Number to Add to List (-1 to Exit): " ); input = keyboard.nextInt(); // Read new Number to add if ( input != -1 ) { arrayList.add( input ); // Add to array if not -1 } } while ( input != -1 ); // call toString method to display list System.out.println( arrayList ); } }

Reference no: EM132153436

Questions Cloud

Long time-wages for low skill workers : After increasing for a long time, wages for low skill workers suddenly started to decrease in the U.S. in the 1850s. Why was this?
Create an equation to divide the number of cookies : Ask the user for the number of cookies per serving using the cin statement. Store this resultin a variable with an appropriate name.
Supply and demand for widgets : Suppose that the supply and demand for widgets is given by the following equations:
Corruption issues mean that not all reaches : Firms and households in many countries typically pay relatively little tax (compared to higher income countries) and receive few benefits
Create and initialize an integer array : Create an add method to insert a unique number into the list. Use the break command to exit a loop.
Will there be a difference in the number or type of persons : Strategic Action. Representatives in the pharmaceutical sales industry deal with two distinct groups. They call on physicians, attempting to convince them.
What could cause a low-income country : Generally speaking, higher income countries tend to have less income inequality than low-income countries, however this does not always hold true.
How does the traveling salesman problem know : How does the traveling salesman problem (TSP) with graphs decide which route is the shortest?
What is the maximum number of values a tree of depth : A tree structure is designed for a specific application where a single node holds until it gets to 5 data elements.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd