How to Handle Duplicate Entries in a Java Array?

In summary: THEN ELSE display an error message and exit the program
  • #1
Rick44
4
0

Homework Statement


Write an application that inputs ten numbers from the user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is a number already entered. If it is a duplicate move on to the next number, if it is unique store the number in the array. After all ten numbers have been entered display the complete set of unique numbers that were entered.



Homework Equations


Im trying to make a reusable class but I don't know how to really do it so I have been just trying everything and I seem to just not get it I need some help to understand this



The Attempt at a Solution


public class Arrayclass {

}
public static void main(String[] args) {

}
public static boolean arrayDupeCheck(int[] array, int dupeNumber) {

}
 
Physics news on Phys.org
  • #2
Rick44 said:

Homework Statement


Write an application that inputs ten numbers from the user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is a number already entered. If it is a duplicate move on to the next number, if it is unique store the number in the array. After all ten numbers have been entered display the complete set of unique numbers that were entered.



Homework Equations


Im trying to make a reusable class but I don't know how to really do it so I have been just trying everything and I seem to just not get it I need some help to understand this
Classes are by nature reusable. Where in the problem description does it say that you have to write a class of any kind?


Rick44 said:

The Attempt at a Solution


public class Arrayclass {

}
public static void main(String[] args) {

}
public static boolean arrayDupeCheck(int[] array, int dupeNumber) {

}
 
  • #3
In this problem I need to make a reusable class and a test class the resuable class and the test class are two diffrent classes that make the program work I am new to netbeans programming and this is something that is confusing to me one file will be declaring the varibales and the other is the run file i hope this helps as i said I am new to this and looking for help trying to understand this myself
My program i set up ilike this
Starting the program
import java.util.Scanner
Declare variables
create array with 10 elements
create Scanner object
Allow user to set all numbers in array
Initialize counter
Ask until user enters a unique number
do
User friendly error message if number is the same as another entered
Check to make sure user input valid number
Request new number if any number is negative
while
Call array method to check user input for duplicates & display error message
for
assign user input to index (number) of array
Print all array elements
Method for loop to check for duplicate entries to array
public static boolean
return true
return false

But then I need to make a reusable class that goes with this I am confused on how to start to write a reusable class that goes along with this in a separate file
this is the complete question

(Must be implemented using a one-dimensional array.)

Write an application that inputs ten numbers from the user, each number can be between 10 and 100, inclusive. As each number is read in determine if it is a number already entered. If it is a duplicate move on to the next number, if it is unique store the number in the array. After all ten numbers have been entered display the complete set of unique numbers that were entered.

But it needs a reuasble class then a test class so this would make 2 files to work i worte the program which is one file and it works but I don't understand the part about a reuseable class that goes with it? Am I going to make another file that describes the varibales that make my program work? Thank You Seems like you are all great People here
 
Last edited:
  • #4
Your problem statement says, "Write an application that ..."

It doesn't say anything about classes. Have you given us the complete problem statement?
 
  • #5
This is my Psuedocode for this program

CREATE public class SingleArray

DECLARE number array as integer and set as single dimensional with 10 elements



CREATE public SingleArray and set all numbers/elements to -1



CREATE public void setNumber method to store the values passed in



CREATE public boolean checkValue with val parameter

IF the value entered is between 10 and 100 statement is true

ELSE the statement is false



CREATE public int checkDuplicate and verify if number is already entered

IF value is equal to previous value stored

CONTINUE loop to next value



PRINT a message to the user with all unique numbers entered



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

SINGLE ARRAY TEST PSEUDOCODE

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



IMPORT Scanner for input



CREATE public class SingleArray



CREATE public static void main



CREATE new instance of SingleArray and Scanner



WHILE counter is less than or equal to ten

PROMPT user for next integer



CALL the checkValue method from reusable class



IF the statement returns true



CALL the checkDuplicate method from reusable class



IF the statement also returns true



CALL the method setNumber from the reusable class



ELSE



PRINT the value needs to be between 10 and 100



DISPLAY the Array method from the reusable class



END main method



END SingleArray class

I hope this helps to show what I need help with Thank You
 
Last edited:
  • #6
Please stop describing your class as "reusable" like it means something. Classes are generally reusable, so describing one this way doesn't add any information.

Let's look at your SingleArray class.
Rick44 said:
CREATE public class SingleArray
DECLARE number array as integer and set as single dimensional with 10 elements

CREATE public SingleArray and set all numbers/elements to -1

CREATE public void setNumber method to store the values passed in

CREATE public boolean checkValue with val parameter
IF the value entered is between 10 and 100 statement is true
ELSE the statement is false

CREATE public int checkDuplicate and verify if number is already entered
IF value is equal to previous value stored
CONTINUE loop to next value

PRINT a message to the user with all unique numbers entered
From this information, I infer that the SingleArray class should have a private data member that is a 10-element array of type int. It's not clear to me what the name should be - number?

There should be a default constructor named SingleArray (no arguments), that sets each element of the array to -1.

There should be a public method named setNumber that returns void (i.e., doesn't return a value). Your description of what it should do is to vague for me to understand what it's supposed to do. Should this method have a parameter?

There should be a public method named checkValue that returns a bool value. It has a single parameter named val. If val is between 10 and 100 this method returns true. Otherwise, it returns false. (Not, as you have it "statement is true" and "statement is false".)

There should be a public method named checkDuplicate that returns an int. Your description doesn't say whether it should have a parameter or not, and your description of what it should do is incorrect. My guess is that it should take a parameter, and that it should compare the parameter value to each value already in the array. You don't have any information about what this method should return. The problem description as given to you should have this information.

Your next step should be coming up with algorithms or pseudocode for the class constructor and the three other methods listed above. Be careful to use exactly these names, because your instructor is likely to deduct points if your give them different names.
 
  • #7
Wow ThankYou I learned more form your help than my instructor now i can use this information to help complete my assignment this is a great place for help Thank You
 
  • #8
Rick44 said:
Wow ThankYou I learned more form your help than my instructor now i can use this information to help complete my assignment this is a great place for help Thank You
Glad to help!

One thing that will help you when you communicate with others is USE SOME PUNCTUATION! In what you wrote above, there is not a single punctuation mark. When you start writing your Java code, you are going to need to put semicolons (;) at the end of statements, and you'll need to use commas occasionally.

What you write in English will be easier to understand if you put a period at the end of each sentence.

Also, if you need more help with your Java code, put a [ code ] tag at the beginning and a [ /code ] tag at the end of your code (leave out the spaces, though. These are HTML tags, so if I write them without spaces, they don't show up.
 

What is Java programming?

Java programming is a high-level, object-oriented programming language that is commonly used for building web, desktop, and mobile applications.

What are the key features of Java programming?

The key features of Java programming include its platform independence, object-oriented approach, automatic memory management, and a large standard library of pre-written code for common tasks.

What are the basic components of a Java program?

A Java program consists of classes, methods, variables, and statements. Classes contain methods, which are blocks of code that perform specific tasks. Variables are used to store data, and statements are used to control the flow of the program.

What is the difference between Java and JavaScript?

Java and JavaScript are two completely different programming languages. Java is a high-level, compiled language used for creating applications, while JavaScript is a scripting language used for creating interactive web elements. They also have different syntax and use different development tools.

What are the benefits of learning Java programming?

Learning Java programming can open up many opportunities for developers, as it is widely used in various industries such as finance, healthcare, and gaming. It is also a great language for beginners to learn due to its simple syntax and extensive community support.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
913
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top