d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Paying Fg For Java Homework > One-dimensional Array Question
123Next
Add Reply New Topic New Poll
Member
Posts: 9,624
Joined: Sep 7 2008
Gold: 3,272.00
Oct 31 2014 03:27pm
The code is described as the following:
Use a one-dimensional array to solve the following problem: Write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, display it only if it's not a duplicate of a number already read. Provide for the "worst case," in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user enters each new value.

Paying 500fg for the Java code, post here or PM
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 31 2014 05:19pm
Code

list = []
5.times do
number = rand(5..100)
next if list.include?(number)
list.push(number)
end


This isn't that difficult of a task. Although my code is in ruby it serves well enough as pseudo code for you to do your project. When dealing with a problem, take sets to figure out what you should do. For your homework I would use these steps:

Code
1) Create an array/stack that allows pushing of variables into the array/stack
2) Create your loop for how many times you wish to loop
3) Generate your random number
4) Goto the next iteration if your number is included inside this array/stack (you may have to iterate through it based on its current length if you don't have access to such a checking function)
5) Append the data to the end of the array


If java does not have a push/pop stack like array system you must follow these steps:

Code
1) Create an array of 1 length
2) Create your loop for how many times you wish to loop
3) Generate your random number
4) Goto the next iteration if your number is included inside this array(you may have to iterate through it based on its current length if you don't have access to such a checking function)
5) Create a new array of original array size + 1 and deallocate the original array
6) Place your number inside the newly resized array
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 31 2014 06:57pm
I believe I have followed all the requirements. Please forgive me for not declaring appropriate packages for each class. I hope you don't lose points for that.


Code
public interface INumberReader
{
void readNumbers();
}


Code
public interface INumberReaderConfig
{
int getMaximumNumber();
int getMinimumNumber();
int getAmountOfNumbers();
InputStream getInputer();
PrintStream getPrinter();
}


Code
public interface INumberReaderFactory
{
INumberReader createNumberReader(INumberReaderConfig config);
}


Code
public class StaticNumberReaderConstants
{
public static final int MAXIMUM_NUMBER = 100;
public static final int MINIMUM_NUMBER = 10;
public static final int AMOUNT_OF_NUMBERS = 5;
public static final InputStream INPUTER = System.in;
public static final PrintStream PRINTER = System.out;
}


Code
public class StaticNumberReaderConfig implements INumberReaderConfig
{
public int getMaximumNumber() { return StaticNumberReaderConstants.MAXIMUM_NUMBER; }
public int getMinimumNumber() { return StaticNumberReaderConstants.MINIMUM_NUMBER; }
public int getAmountOfNumbers() { return StaticNumberReaderConstants.AMOUNT_OF_NUMBERS; }
public InputStream getInputer() { return StaticNumberReaderConstants.INPUTER; }
public PrintStream getPrinter() { return StaticNumberReaderConstants.PRINTER; }

}
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 31 2014 06:57pm
Continued...

Code
import java.io.*;
import java.util.Scanner;

public abstract class NumberReaderBase implements INumberReader
{
private Scanner in;
private PrintStream out;
private int maximumNumber, minimumNumber, amountOfNumbers;

public NumberReaderBase(INumberReaderConfig config)
{
readConfig(config);
}

private void readConfig(INumberReaderConfig config)
{
in = new Scanner(config.getInputer());
out = config.getPrinter();
maximumNumber = config.getMaximumNumber();
minimumNumber = config.getMinimumNumber();
amountOfNumbers = config.getAmountOfNumbers();
}

protected int getMaximumNumber() { return maximumNumber; }
protected int getMinimumNumber() { return minimumNumber; }
protected int getAmountOfNumbers() { return amountOfNumbers; }

protected int readNumber()
{
String msg = String.format("Input a number between %d and %d: ", minimumNumber, maximumNumber);

int number = 0;

do
{
out.println(msg);
number = in.nextInt();

} while(!isValidNumber(number));

return number;
}

protected boolean isValidNumber(int number)
{
return number >= minimumNumber && number <= maximumNumber;
}

protected void printLine(String line)
{
out.println(line);
}

}


Code
public class UniqueNumberReader extends NumberReaderBase
{
private int[] uniqueNumbers;
private int numUniqueNumbers;


public UniqueNumberReader(INumberReaderConfig config)
{
super(config);
}

public void readNumbers()
{
int capacity = getAmountOfNumbers();
uniqueNumbers = new int[capacity];

while(capacity-- > 0)
readUniqueNumber();

}

private boolean isUniqueNumber(int number)
{
for(int i = 0; i < numUniqueNumbers; i++)
if(uniqueNumbers[i] == number)
return false;
return true;
}

protected void readUniqueNumber()
{
int number = readNumber();

if(isUniqueNumber(number))
storeUniqueNumber(number);

}

protected int[] getUniqueNumbers()
{
int[] uniques = new int[numUniqueNumbers];
for(int i = 0; i < uniques.length; i++)
uniques[i] = uniqueNumbers[i];

return uniques;
}

protected void storeUniqueNumber(int number)
{
uniqueNumbers[numUniqueNumbers++] = number;
}
}


Code
public class VerboseUniqueNumberReader extends UniqueNumberReader
{
public VerboseUniqueNumberReader(INumberReaderConfig config)
{
super(config);
}
protected void readUniqueNumber()
{
super.readUniqueNumber();

printUniqueNumbers();

}

protected void storeUniqueNumber(int number)
{
super.storeUniqueNumber(number);

printUniqueNumber(number);
}

private void printUniqueNumbers()
{
int[] uniques = getUniqueNumbers();

String uniqueList = new String("Unique List:");
for(int i = 0; i < uniques.length; i++)
uniqueList = String.format("%s %s",uniqueList,String.valueOf(uniques[i]));

printLine(uniqueList);

}

private void printUniqueNumber(int number)
{
String unique = new String("Unique Number:");

unique = String.format("%s %s",unique, number);

printLine(unique);

}
}


Code
public class UniqueNumberReaderFactory implements INumberReaderFactory
{
public INumberReader createNumberReader(INumberReaderConfig config)
{
return new UniqueNumberReader(config);
}
}


Code
public class VerboseUniqueNumberReaderFactory extends UniqueNumberReaderFactory
{
public INumberReader createNumberReader(INumberReaderConfig config)
{
return new VerboseUniqueNumberReader(config);
}
}

Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 31 2014 06:58pm
Continued...

Code
public abstract class NumberReaderApplicationBase
{
private INumberReaderFactory numberReaderFactory;
private INumberReaderConfig numberReaderConfig;

public NumberReaderApplicationBase(INumberReaderFactory numberReaderFactory, INumberReaderConfig numberReaderConfig)
{
this.numberReaderFactory = numberReaderFactory;
this.numberReaderConfig = numberReaderConfig;
}

public void execute()
{
INumberReader nr = numberReaderFactory.createNumberReader(numberReaderConfig);

nr.readNumbers();
}
}


Code
public class VerboseUniqueNumberReaderApplication extends NumberReaderApplicationBase
{
VerboseUniqueNumberReaderApplication()
{
super(new VerboseUniqueNumberReaderFactory(), new StaticNumberReaderConfig());
}
}


Code
public class VerboseUniqueNumberReaderApplicationDriver
{
public static void main(String[] args)
{
new VerboseUniqueNumberReaderApplication().execute();
}
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 31 2014 07:17pm
Vouch, minkos code compiles and runs fine.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 31 2014 07:24pm
Take your C# naming conventions out of the java thread. Do you want OP to follow your useful habit of prefixing interface names with I? He'll never hear the end of it from his teacher. You should never violate standards to make code more readable when you're paid by the hour.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 31 2014 07:31pm
Quote (carteblanche @ Oct 31 2014 08:24pm)
Take your C# naming conventions out of the java thread. Do you want OP to follow your useful habit of prefixing interface names with I? He'll never hear the end of it from his teacher. You should never violate standards to make code more readable when you're paid by the hour.


Eh, there are arguments both ways. Java enthusiasts are agaisnt it because its a bit redundant and can make class names a bit ridic. It has become a habit for me because of .NET. At times it is still annoying, but I do admit, I like looking at a class declaration and knowing what class I am extending and what interface I am implementing just by the name. Same thing when a method implementation has

Code
List<IAnimal>


as opposed to

Code
List<Animal>


It just makes it easier to digest at quick glance.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 31 2014 07:45pm
Quote (Minkomonster @ Oct 31 2014 09:31pm)
Eh, there are arguments both ways. Java enthusiasts are agaisnt it because its a bit redundant and can make class names a bit ridic. It has become a habit for me because of .NET. At times it is still annoying, but I do admit, I like looking at a class declaration and knowing what class I am extending and what interface I am implementing just by the name. Same thing when a method implementation has

Code
List<IAnimal>


as opposed to

Code
List<Animal>


It just makes it easier to digest at quick glance.


i prefer it too since i came from a .net background as well. coworker frowned on me for using it on our java project. i have a habit of using hungarian notation for UI and prefixing object fields with underscore. i find it very frustrating when you have a label, textbox, object fields, and local variable to handle a field (eg: userName), and you want to name them all the same thing. whereas i can easily tell at a glance when i name them: lblUserName, txtUserName, _userName, userName. coworker hates the _ so much that i see him do this when he adds code to my js files:

before:
var _log = new Log('FileName');

after:
var _log = new Log('FileName');
var log = _log;

This post was edited by carteblanche on Oct 31 2014 07:47pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 31 2014 07:50pm
Quote (carteblanche @ Oct 31 2014 08:45pm)
i prefer it too since i came from a .net background as well. coworker frowned on me for using it on our java project. i have a habit of using hungarian notation for UI and prefixing object fields with underscore. i find it very frustrating when you have a label, textbox, object fields, and local variable to handle a field (eg: userName), and you want to name them all the same thing. whereas i can easily tell at a glance when i name them: lblUserName, txtUserName, _userName, userName. coworker hates the _ so much that i see him do this when he adds code to my js files:

before:
var _log = new Log('FileName');

after:
var _log = new Log('FileName');
var log = _log;


Your coworker sounds petty. Do you not have a tech lead or an architect maintaining code standards?
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll