EMB1006 JAVA Part 2 - Department of Computer Science

advertisement
EMB1006 JAVA Part 2
Arrays and onwards
Jonathan-Lee
Jones
Overview
•
•
•
•
Array Recap
Using an Array Example
Finding the lowest and highest values in an array
Defensive Programming
What is an Array
• As mentioned previously, an array is a method for
storing multiple values that are linked together.
• The name of the array is a pointer to the first
address in memory.
• An array can be of any type, primitive or object
(even user defined object for example student
records) and can even be made up of other arrays
to give a 2 dimensional, 3 dimensional or even
greater array.
An Example Array
• Arrays can be initialised as empty arrays, or just
assigned values.
• To initialise as an empty array we use the following
structure:
• Int N = 100;
• Int[] testArray = new int[N];
• To Initialise by assigning values we use the
following structure:
• Int[] testArray = {1,2,3,4,5,6,7,8,9};
An Example Array
class Deck {
public static void main(final String args[]) {
final String suit[] = {"Clubs", "Diamonds", "Hearts",
"Spades" };
final String rank[] = {"2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King", "Ace" };
final int nr_suits = suit.length;
final int nr_ranks = rank.length;
final int nr_cards = nr_suits * nr_ranks;
final String deck[] = new String[nr_cards];
for (int r = 0, base_index = 0; r < nr_ranks; ++r,
base_index+=nr_suits) {
for (int s=0; s < nr_suits; ++s)
deck[base_index+s] = rank[r] + " of " + suit[s];
}
}
}
Shuffling the Deck Using an Array
for (int i = 0; i < nr_cards; ++i) {
final int rand = i + (int) (Math.random() * (nr_cards-i));
final String t = deck[rand];
deck[rand] = deck[i];
deck[i] = t;
}
• The above code snippet will perform a shuffle on the deck. This is the best
method to use with the tools you have for creating a mathematically fair shuffle.
You can test this if you want! Below is how you print contents of array (remember
printing out the name just prints the memory address pointer!)
for (int i = 0; i < nr_cards; ++i) System.out.println(deck[i]);
Finding the Lowest and Highest
Values
Write a short piece of code to find both the highest and lowest numbers from an array of
size N, and print these out, alongside their position in the array.
You may need the following:Integer.MIN_VALUE;
Integer.MAX_VALUE;
Array.length;
For Loop
Integer.parseInt();
Finding the Lowest and Highest
Values
class minmax{
public static void main(String[] args){
int N = args.length;
int[]numbers = new int[N];
What would happen if
zero arguments were
entered?
What else could go
wrong?
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int maxPos =0;
int minPos =0;
for(int i=0;i<N;++i){
numbers[i]=Integer.parseInt(args[i]);
if (numbers[i]>max){
max=numbers[i];
maxPos=i; }
if (numbers[i]<min){
min=numbers[i];
minPos=i;}}
System.out.println("Max = " + max + " at position " + maxPos +".");
System.out.println("Min = " + min + " at position " + minPos +"."); }}
Validating Input, Defensive programming
• When you allow the user to input values into the program, you inevitably invite
errors at run time. If you ask for an int and they give a string for example. A large
number of these can be avoided by using various scanner tools, but how would
you validate command line input?
• You are tasked with writing a program to take in 3 numbers from the command
line, then add the first 2 together then divide by the third. It is a trivial task, but you
would be surprised how many problems may occur.
• Try to make sure that the program will not crash by throwing a run time exception,
and also the program gives the expected values. What do you need to check to
ensure this.
Validating Input, Defensive programming
class threeNums{
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
double ans = (a+b)/c;
System.out.println(“ans = ” + ans);
}
}
Validating Input, Defensive programming
lass threeNums{
public static void main(final String[] args){
final int a = Integer.parseInt(args[0]);
final int b = Integer.parseInt(args[1]);
final int c = Integer.parseInt(args[2]);
double ans = (a+b)/c;
System.out.println("ans = " + ans);
}
}
Validating Input, Defensive programming
Possible Errors that can occur:1. Not enough input arguments (program requires 3, what happens if it gets 2?)
2. If the third input is 0 what will happen?
3. What happens if a= 2,000,000,000 and b = 1,999,999,999?
4. What happens if the user inputs the following values by mistake:- 4 8u 22?
Is there anything else that can go wrong with this code?
What if the following numbers are input 1, 2 & 2?
What output do you expect, and what do you get?
Download