Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 584f477

Browse files
committed
code for arry slide deck
1 parent bea9203 commit 584f477

File tree

8 files changed

+258
-1
lines changed

8 files changed

+258
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ assessments/
88
*.log
99
*~
1010
*.swp
11-
fromslides/S12*
1211
fromslides/S13*
1312
fromslides/S14*
1413
fromslides/S14*

fromslides/S12-Arrays/Array1.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//package fromslides.S12;
2+
import java.util.Scanner;
3+
/**
4+
* This class illustrates exercises from the slide deck for the course
5+
* 360-420-DW Intro to Java
6+
* @author PMCampbell
7+
* @version today
8+
**/
9+
public class Array1 {
10+
public static void main(String[] args) {
11+
// declare int variables (memory reserved for each)
12+
int num1, num2, num3;
13+
int sum, mult;
14+
// declare int array variable (memory reserved for the address of the array)
15+
int num[];
16+
// instantiate the array (reserve heap memory for it)
17+
num = new int[3];
18+
19+
// assign integer literals to int variables
20+
num1 = 5;
21+
num2 = 10;
22+
num3 = 15;
23+
24+
// use int variables
25+
sum = num1 + num2 + num3;
26+
mult = num1 * num2 * num3;
27+
28+
System.out.printf("The sum of %d + %d + %d is %d\n",
29+
num1, num2, num3, sum);
30+
System.out.printf("The product of %d * %d * %d is %d\n",
31+
num1, num2, num3, mult);
32+
33+
// assign integer literals to int array elements
34+
num[0] = 5;
35+
num[1] = 10;
36+
num[2] = 15;
37+
// use int variables
38+
sum = num[0] + num[1] + num[2];
39+
mult = num[0] * num[1] * num[2];
40+
41+
System.out.printf("The sum of %d + %d + %d is %d\n",
42+
num[0], num[1], num[2], sum);
43+
System.out.printf("The product of %d * %d * %d is %d\n",
44+
num[0], num[1], num[2], mult);
45+
46+
47+
} //main()
48+
} //

fromslides/S12-Arrays/Array2.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package fromslides.S13;
2+
import java.util.Scanner;
3+
public class Array2 {
4+
public static void main(String[] args) {
5+
// declare and instantiate
6+
// declare = (memory reserved for the address of the array)
7+
// instantiate = (memory reserved for the elements in the array)
8+
9+
int daysInMonth[] = {31, 28,31,30,31,30,31,31,30,31,30,31};
10+
int months[] = {1,2,3,4,5,6,7,8,9,10,11,12};
11+
String monthsString[] = { "Jan", "Feb", "Mar", "Apr", "May",
12+
"Jun", "Jul", "Aug", "Sep", "Oct",
13+
"Nov", "Dec"};
14+
int month;
15+
Scanner kb = new Scanner(System.in);
16+
17+
System.out.print("Enter a month: ");
18+
month = (kb.nextInt() - 1);
19+
20+
System.out.printf("%s has %d days\n", monthsString[month], daysInMonth[month]);
21+
22+
} //main()
23+
} //
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package fromslides.S13;
2+
import java.util.Scanner;
3+
public class ArrayCopy {
4+
public static void main(String[] args) {
5+
6+
int array[] = { 2, 4, 6, 8, 10 };
7+
int array2[];
8+
9+
// shallow copy, both arrays refer to the same area in heap
10+
array2 = array;
11+
System.out.println("shallow copied array:");
12+
System.out.println("array[2] contains " + array[2]);
13+
System.out.println("array2[2] contains " + array2[2]);
14+
15+
array[2] = 99;
16+
array2[2] = 5;
17+
System.out.println("after mod array[2] = 99 and array2[2] = 5");
18+
System.out.println("array[2] contains " + array[2]);
19+
System.out.println("array2[2] contains " + array2[2]);
20+
21+
// deep copy, different area in heap for each array
22+
System.out.println("deep copied array:");
23+
array2 = new int[5];
24+
for (int i = 0; i < 5; i++) {
25+
array2[i] = array[i];
26+
}
27+
System.out.println("array[2] contains " + array[2]);
28+
System.out.println("array2[2] contains " + array2[2]);
29+
30+
array[2] = 99;
31+
array2[2] = 15;
32+
System.out.println("after mod array[2] = 99 and array2[2] = 15");
33+
System.out.println("array[2] contains " + array[2]);
34+
System.out.println("array2[2] contains " + array2[2]);
35+
36+
} //main()
37+
} // ArraySize
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fromslides.S13;
2+
import java.util.Scanner;
3+
public class ArraySize {
4+
public static void main(String[] args) {
5+
// declare and instantiate
6+
// declare = (memory reserved for the address of the array)
7+
// instantiate = (memory reserved for the elements in the array)
8+
9+
int numTests, tests[], sum=0, count;
10+
11+
Scanner kb = new Scanner(System.in);
12+
13+
System.out.print("How Many Tests? ");
14+
numTests = kb.nextInt();
15+
16+
tests = new int[numTests];
17+
18+
count = 0;
19+
while (count < tests.length) {
20+
System.out.printf("Mark for test #%d: ", count+1);
21+
tests[count] = kb.nextInt();
22+
count++;
23+
}
24+
for (int i=0; i < tests.length; i++) {
25+
sum += tests[i];
26+
}
27+
System.out.printf("Average %d of %d tests\n",
28+
sum/tests.length, tests.length);
29+
30+
} //main()
31+
} // ArraySize

fromslides/S12-Arrays/Numbers.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// remove line or change packageName to reflect your directory structure
2+
//package packageName;
3+
// needed if using Scanner
4+
import java.util.Scanner;
5+
/**
6+
* read in 5 numbers give the sum/ average
7+
* @author P.M.Campbell
8+
* @version today
9+
**/
10+
11+
public class Numbers
12+
{
13+
public static void main(String[] args)
14+
{
15+
int num1, num2,num3,num4, num5;
16+
Scanner kb = new Scanner(System.in);
17+
18+
// read in 5 numbers
19+
// for ( init ; condition ; change cond )
20+
// for (int i=0; i < 5 ; i++) {
21+
System.out.println("Enter 5 numbers:");
22+
num1 = kb.nextInt();
23+
num2 = kb.nextInt();
24+
num3 = kb.nextInt();
25+
num4 = kb.nextInt();
26+
num5 = kb.nextInt();
27+
// average & sum
28+
System.out.println("Average "+
29+
(num1+num2+num3+num4+num5)/5);
30+
System.out.println("Sum "+
31+
(num1+num2+num3+num4+num5) );
32+
} // main()
33+
} // Numbers class
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// remove line or change packageName to reflect your directory structure
2+
//package packageName;
3+
// needed if using Scanner
4+
import java.util.Scanner;
5+
/**
6+
* read in 5 numbers give the sum/ average
7+
* using an array
8+
* @author P.M.Campbell
9+
* @version today
10+
**/
11+
12+
// reference == pointer == address
13+
public class NumbersArray
14+
{
15+
public static void main(String[] args)
16+
{
17+
//int num1, num2,num3,num4, num5;
18+
final int ARRAY_SIZE = 3;
19+
int num[]; // declare array (pointer in stack)
20+
int average=0, sum=0;
21+
Scanner kb = new Scanner(System.in);
22+
23+
num = new int[ARRAY_SIZE]; // instantiate array (space in heap)
24+
// read in 5 numbers
25+
// for ( init ; condition ; change cond )
26+
for (int i=0; i < ARRAY_SIZE ; i++) {
27+
System.out.println("Enter a number:");
28+
num[i] = kb.nextInt();
29+
System.out.println("DEBUG: i: "+i +
30+
" num[i] " + num[i] );
31+
}
32+
// average & sum
33+
for (int i=0; i < ARRAY_SIZE ; i++) {
34+
sum += num[i];
35+
}
36+
average = sum/ARRAY_SIZE;
37+
System.out.println("Average "+average);
38+
System.out.println("Sum "+sum);
39+
40+
} // main()
41+
} // Numbers class
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// remove line or change packageName to reflect your directory structure
2+
//package packageName;
3+
// needed if using Scanner
4+
import java.util.Scanner;
5+
/**
6+
* read in 5 numbers give the sum/ average
7+
* using an array
8+
* @author P.M.Campbell
9+
* @version today
10+
**/
11+
12+
// reference == pointer == address
13+
public class NumbersArrayUserCount
14+
{
15+
public static void main(String[] args)
16+
{
17+
//int num1, num2,num3,num4, num5;
18+
final int ARRAY_SIZE = 3;
19+
int entries;
20+
int num[]; // declare array (pointer in stack)
21+
int average=0, sum=0;
22+
Scanner kb = new Scanner(System.in);
23+
24+
System.out.print("How many numbers do you want to sum today? ");
25+
entries = kb.nextInt();
26+
27+
num = new int[entries]; // instantiate array (space in heap)
28+
// read in 5 numbers
29+
// for ( init ; condition ; change cond )
30+
for (int i=0; i < entries ; i++) {
31+
System.out.println("Enter a number:");
32+
num[i] = kb.nextInt();
33+
System.out.println("DEBUG: i: "+i +
34+
" num[i] " + num[i] );
35+
}
36+
// average & sum
37+
for (int i=0; i < entries ; i++) {
38+
sum += num[i];
39+
}
40+
average = sum/entries;
41+
System.out.println("Average "+average);
42+
System.out.println("Sum "+sum);
43+
44+
} // main()
45+
} // Numbers class

0 commit comments

Comments
 (0)