Skip to content

Commit

Permalink
submit part03_11->15
Browse files Browse the repository at this point in the history
  • Loading branch information
truongvantuan committed Feb 8, 2021
1 parent 47af361 commit 52ce201
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@
/part03-Part03_07.RememberTheseNumbers/target/
/part03-Part03_08.OnlyTheseNumbers/target/
/part03-Part03_09.GreatestInList/target/
/part03-Part03_10.IndexOf/target/
/part03-Part03_10.IndexOf/target/
/part03-Part03_11.IndexOfSmallest/target/
/part03-Part03_12.SumOfAList/target/
/part03-Part03_13.AverageOfAList/target/
/part03-Part03_14.OnTheList/target/
/part03-Part03_15.PrintInRange/target/
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,41 @@ public static void main(String[] args) {

// implement here a program that reads user input
// until the user enters 9999

// after that, the program prints the smallest number
// and its index -- the smallest number
// might appear multiple times
ArrayList<Integer> list = new ArrayList<>();
int smallest;
while (true) {
int input = scanner.nextInt();


if (input == 9999) {
smallest = list.get(0);
break;
}

list.add(input);
}

smallest = smallestKey(list);

System.out.println("Smallest number: " + smallest);

for (int i = 0; i < list.size(); i++) {
if (smallest == list.get(i)) {
System.out.println("Found at index: " + i);
}
}
}

public static int smallestKey(ArrayList<Integer> list) {
int smallest = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (smallest > list.get(i)) {
smallest = list.get(i);
}
}
return smallest;
}

}
8 changes: 5 additions & 3 deletions part03-Part03_12.SumOfAList/src/main/java/SumOfAList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public static void main(String[] args) {
list.add(input);
}

System.out.println("");

// toteuta listan lukujen summan laskeminen tänne
int sum = 0;
for (Integer value : list) {
sum += value;
}
System.out.println("Sum: " + sum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ public static void main(String[] args) {
// adding them on a list until user gives -1.
// Then it computes the average of the numbers on the list
// and prints it.

int sum = 0;
ArrayList<Integer> listInt = new ArrayList<>();

while (true) {
int value = Integer.valueOf(scanner.nextLine());
if (value == -1) {
break;
}
listInt.add(value);
sum += value;
}

System.out.println("Average: " + (double) sum / listInt.size());
}
}
14 changes: 14 additions & 0 deletions part03-Part03_14.OnTheList/src/main/java/OnTheList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

ArrayList<String> list = new ArrayList<>();

while (true) {
String input = scanner.nextLine();
if (input.equals("")) {
Expand All @@ -17,5 +18,18 @@ public static void main(String[] args) {
list.add(input);
}

System.out.print("Search for? ");
String key = scanner.nextLine();

boolean found = list.contains(key);

System.out.println(found);

if (found) {
System.out.println(key + " was found!");
} else {
System.out.println(key + " was not found!");

}
}
}
24 changes: 23 additions & 1 deletion part03-Part03_15.PrintInRange/src/main/java/PrintInRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ public class PrintInRange {

public static void main(String[] args) {
// Try your method here

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(2);
numbers.add(6);
numbers.add(-1);
numbers.add(5);
numbers.add(1);

System.out.println("The numbers in the range [0, 5]");
printNumbersInRange(numbers, 0, 5);

System.out.println("The numbers in the range [3, 10]");
printNumbersInRange(numbers, 3, 10);
}


public static void printNumbersInRange(ArrayList<Integer> numbers, int lowerLimit, int upperLimit) {
for (Integer item : numbers) {
if (item >= lowerLimit && item <= upperLimit) {
System.out.println(item);
}
}
}

}

0 comments on commit 52ce201

Please sign in to comment.