Comp Sci How to Handle Duplicate Entries in a Java Array?

  • Thread starter Thread starter Rick44
  • Start date Start date
  • Tags Tags
    Java Programming
AI Thread Summary
The discussion centers on creating a Java application that inputs ten unique numbers between 10 and 100, storing them in an array while checking for duplicates. Participants express confusion about implementing a reusable class alongside the main application, as the homework statement does not explicitly require class structures. Suggestions include defining a `SingleArray` class with methods for setting numbers, checking valid input, and verifying duplicates. Clarifications emphasize the importance of clear method definitions and proper coding syntax. The conversation concludes with encouragement and advice on effective communication and coding practices.
Rick44
Messages
4
Reaction score
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
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) {

}
 
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:
Your problem statement says, "Write an application that ..."

It doesn't say anything about classes. Have you given us the complete problem statement?
 
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:
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.
 
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
 
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.
 

Similar threads

Replies
12
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
7
Views
2K
Replies
7
Views
3K
Replies
1
Views
1K
Replies
5
Views
3K
Back
Top