0% found this document useful (0 votes)
3 views24 pages

Chapter 7

Chapter 7 covers various programming concepts including loops, arrays, and methods in Java. It provides examples of counting vowels in user input, iterating through arrays, and searching for minimum values. Additionally, it discusses parallel arrays, lake lookup functionality, and array sorting techniques.

Uploaded by

kenzhao0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Chapter 7

Chapter 7 covers various programming concepts including loops, arrays, and methods in Java. It provides examples of counting vowels in user input, iterating through arrays, and searching for minimum values. Additionally, it discusses parallel arrays, lake lookup functionality, and array sorting techniques.

Uploaded by

kenzhao0621
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 7

CS 200 - Programming I - Summer 2020


Mark Powers
Another Loop Example
● The user can enter any number of text lines. How many of each vowel is in
the text? How many total characters? The user ends by typing a blank line.
● Please enter some text:
Total: 312 characters
A: 20
E: 32
I: 18
O: 14
U: 9
● Write a method that returns the output as a string, instead of printing it
● How many variables do we need?
Arrays
● Fixed size list of data, to access the data you must get an index
● .length to get the size of the array
● int x = 10; x 10

● int[] a = new int[5];


a[0] = 10; a 10
15
a[1] = 15; 20
a[2] = 20; 25
a[3] = 25; 30
a[4] = 30;
[Link](a[3]); stack heap
Arrays
● Fixed size list of data, to access the data you must get an index
● .length to get the size of the array
● int x = 10; x 10

● int[] a = { 10, 15, 20, 25, 30 };


[Link](a[3]); a 10
15
20
25
30

stack heap
Iterating through arrays
● First index is always 0, last index is [Link] - 1
● int arr = new int[3];

arr

0 1 2
Iterating through arrays
● First index is always 0, last index is [Link] - 1
● int arr = new int[3];

arr

0 1 2
for(int index = 0; index < [Link]; index++){
[Link](arr[i]);
}
Iterating through arrays
● First index is always 0, last index is [Link] - 1
● int arr = new int[3];

arr

0 1 2
for(int index = 0; index <= [Link]; index++){
[Link](arr[i]);
}
Printing arrays
● int [] a = {1, 2, 3};
[Link](a);
Printing arrays
● int [] a = {1, 2, 3};
[Link](a);

for(int i = 0; i < [Link]; i ++){


[Link](a[i]);
}
Printing arrays
● int [] a = {1, 2, 3};
[Link](a);

for(int i = 0; i < [Link]; i ++){


[Link](a[i]);
}

[Link]([Link](a));
Printing arrays
● int [] a = {1, 2, 3};
[Link](a);

for(int i = 0; i < [Link]; i ++){


[Link](a[i]);
}

[Link]([Link](a));
● Example: Write your own code to do [Link](a);
Another Loop Example
● The user can enter any number of text lines. How many of each vowel is in
the text? How many total characters?
● Please enter some text:
Total: 312 characters
A: 20
E: 32
I: 18
O: 14
U: 9
● Write a method that returns the output as a string, instead of printing it
Scanner scnr = new Scanner([Link]);
int count = 0;
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
while([Link]()){
String next = [Link]();
next = [Link]();
for(int i = 0; i < [Link](); i++){
count++;
switch([Link](i)){
case 'A':
countA++;
break;
case 'E':
countE++;
break;
case 'I':
countI++;
break;
case 'O':
countO++;
break;
case 'U':
countU++;
break;
}
}
}
[Link]("Total: " + count);
[Link]("A: " + countA);
[Link]("E: " + countE);
[Link]("I: " + countI);
[Link]("O: " + countO);
[Link]("U: " + countU);
How to extend to all letters?
● Each character has an integer value
● Use this value to find the index
● What size array should we use?
import [Link];
public class LetterCounter {
public static void main(String[] args) {
Scanner scnr = new Scanner([Link]);
int totalChars = 0;
int[] charCounts = new int[100];
String line = [Link]().toUpperCase();
while (![Link]("")) {
// do something with line
for (int i = 0; i < [Link](); i++) {
totalChars++;
char currentChar = [Link](i);
charCounts[ (int)currentChar ]++;
}
// get the next line
line = [Link]().toUpperCase();
}
// Print out total characters
[Link]("Total Characters: " + totalChars);
for(int i = ( (int)'A' ); i <= ( (int) 'Z' ); i++) {
[Link]( ( (char)i ) + ": " + charCounts[i] );
}
// [Link]([Link](charCounts));
}
}
Searching
● Iterate through an array, check if value is what we want
● public int findMin(int[] array){
int minimumSoFar = ???; // What should we put here
for(int i = 0; i < [Link]; i++){
if(array[i] < minimumSoFar){
minimumSoFar = array[i];
}
}
return minimumSoFar;
}
Searching
● Iterate through an array, check if value is what we want
● public int findMin(int[] array){
int minimumSoFar = array[0];
for(int i = 0; i < [Link]; i++){
if(array[i] < minimumSoFar){
minimumSoFar = array[i];
}
}
return minimumSoFar;
}
Searching
● Maybe we want to know the location of the minimum
● public int findMinIndex(int[] array){
int minimumIndexSoFar = 0
for(int i = 0; i < [Link]; i++){
if(array[i] < array[minimumIndexSoFar]){
minimumIndexSoFar = i;
}
}
return minimumIndexSoFar;
}
Parallel arrays
● We can keep multiple arrays to store different types of data
● The same index in each array represents one unit

String[] items = {“apple”, “banana”, “orange”};


double[] prices = {0.4, 0.19, 0.25};

● Write a program to print items with the prices


● apple: $0.4
banana: $0.19
orange: $0.25
County lakes look up
● [Link]
Lake Name County Size (Acres)
Lake Kegonsa Dane 3200
Lake Koshkonong Dane 10595
Lake Mendota Dane 9781
Lake Monona Dane 3359
Lake Waubesa Dane 2074
Lake Wingra Dane 336
● Store name and sq miles (sq miles = ac * 0.0015625)
● Update the program so when they type in the name of a lake, it prints the sq
miles of the lake
● Please enter a lake: Kegonsa
Lake Kegonsa is 5 sq miles
import [Link];

public class LakeLookup {


public static void main(String[] args) {
Scanner scnr = new Scanner([Link]);
[Link]();

String[] names = new String[6];


double[] areas = new double[6];

for (int i = 0; i < 6; i++) {


[Link]();
String name = [Link]();
[Link]();
int acres = [Link]();
double sqMiles = acres * 0.0015625;

names[i] = name;
areas[i] = sqMiles;
}

while (true) {
[Link]("Please enter a lake name: ");
String lookupName = [Link]();

if([Link]("quit")) {
break;
}

// find the lake index with the same name


int index = findIndexOfName(names, lookupName);
if (index == -1) {
[Link]("That lake doesn't exist!");
} else {
// print out its area
[Link]("Lake " + lookupName + " is " + areas[index] + " sq miles");
}
}

public static int findIndexOfName(String[] names, String lookup) {


for (int i = 0; i < [Link]; i++) {
if (names[i].equals(lookup)) {
return i;
}
}
return -1;
}
}
Example - Array equality
● How do you check if two arrays have the same entries?
● Remember == on reference types checks if the references are equal, not if
what is pointed to by the references is the same
Array sorting
● Write a program that sorts an array.
● Idea:
○ If two adjacent elements are out of order, flip them
○ Repeat until all elements are in order
Fixed Size
● Write a program that stores numbers from input until it encounters a negative
number.
○ What size array should be used?
○ We must try a size, and if we need to recopy the array

You might also like