Homework 8 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
while
loop like this
Scanner input = new Scanner(file); while (input.hasNextLine()){ System.out.println(input.nextLine()); }
import java.io.*; import java.util.Scanner; public class ReadFields { public static void main (String[] args) throws FileNotFoundException { File file = new File(args[0]); Scanner input = new Scanner(file); String line; String [] fields; while (input.hasNextLine()){ line = input.nextLine(); fields = line.split(" "); for (int i = 0; i < fields.length; i++) System.out.print(fields[i] + " "); System.out.println(); } } }
public class Cheer1 { public static void main (String[] args){ cheer("Sox"); } public static void cheer(String team){ System.out.println("Go " + team + "!"); } } $ java Cheer1 Go Sox!
public class Cheer2 { public static void main (String[] args){ cheer(); } public static void cheer(){ String team = "Sox"; System.out.println("Go " + team + "!"); } } $ java Cheer2 Go Sox!
public class Cheer3 { public static void main (String[] args){ print_cheer("Sox"); } public static void print_cheer(String team){ System.out.println(cheer(team)); } public static String cheer(String team){ return "Go " + team + "!"; } }
public class Cheer4 { public static void main (String[] args){ String team = "Pats"; cheer(team); System.out.println(team); } public static void cheer(String team){ System.out.println("Go " + team + "!"); } } $ java Cheer4 Go Pats! Pats
ArrayList<String> list = new ArrayList<>();
import
statement
at the top of your code
import java.util.ArrayList;
import java.util.ArrayList; public class ArrayListString { public static void main (String[] args){ ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < args.length; i++){ list.add(args[i]); } System.out.println(list); } } $ java ArrayListString foo bar blecth [foo, bar, blecth]
System.out.println()
import java.util.ArrayList;
public class ArrayListString2 {
public static void main (String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
list.add("three");
list.add("four");
list.add("five");
System.out.println("The length of the list is " + list.size);
}
}
$ java ArrayListString2
The length of the list is 5
for
loop to move through
the values
import java.util.ArrayList; public class ArrayListString3 { public static void main (String[] args){ ArrayList<String> list = new ArrayList<>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); list.add("five"); for (int i = 0; i < list.size(); i++){ System.out.println(list.get(i)); } } } $ java ArrayListString3 one two three four five
int
and double
are primitives
not objects
int
or double
ArrayList<int> list = new ArrayList<>();we write
ArrayList<Integer> list = new ArrayList<>();
int
int num = Integer.parseInt(args[0]);
public class IntegerCreate {
public static void main (String[] args){
Integer number = new Integer(5);
System.out.println(number);
}
}
$ java IntegerCreate
5
int
int
variable like this
int number = 5;we can create an Integer variable like this
Integer number = 5;
public class IntegerCreate2 { public static void main (String[] args){ Integer number = 5; System.out.println(number); } } $ java IntegerCreate2.java 5
for
loopimport java.util.ArrayList; public class ArrayListInteger { public static void main (String[] args){ ArrayList<Integer> list = new ArrayList<>(); for (int i = 1; i <= 10; i++){ list.add(i); } System.out.println(list); } } $ java ArrayListInteger [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
import java.util.*; import java.io.*; public class ArrayListIntegerFile { public static void main (String[] args) throws FileNotFoundException { ArrayList<Integer> numbers = new ArrayList<>(); File file = new File(args[0]); Scanner input = new Scanner(file); while (input.hasNextInt()){ numbers.add(input.nextInt()); } System.out.println(numbers.size()); } } $ java ArrayListIntegerFile numbers.txt 30
import java.util.*;
import java.io.*;
public class AverageArrayList {
public static void main (String[] args)
throws FileNotFoundException {
ArrayList<Integer> numbers = new ArrayList<>();
File file = new File(args[0]);
Scanner input = new Scanner(file);
while (input.hasNextInt()){
numbers.add(input.nextInt());
}
System.out.println(average(numbers));
}
public static int average(ArrayList<Integer> numbers){
int total = 0;
for (int i = 0; i < numbers.size(); i++){
total += numbers.get(i);
}
return total/numbers.size();
}
}
$ java AverageArrayList numbers.txt
26
public static int above_average(ArrayList<Integer> numbers, int average){ int numbers_above = 0; for (int i = 0; i < numbers.size(); i++){ if (numbers.get(i) > average) { numbers_above += 1; } } return numbers_above; }
public static int below_average(ArrayList<Integer> numbers, int average){ int numbers_below = 0; for (int i = 0; i < numbers.size(); i++){ if (numbers.get(i) < average) { numbers_below += 1; } } return numbers_below; }
Contribute to society and to human well-being, acknowledging that all people are stakeholders in computing