Skip to content

Instantly share code, notes, and snippets.

@rjlutz
Created July 17, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjlutz/69bce4dfebcbf856abe0f3e763d9f800 to your computer and use it in GitHub Desktop.
Save rjlutz/69bce4dfebcbf856abe0f3e763d9f800 to your computer and use it in GitHub Desktop.
Single Dimensional Arrays (Chapter 7) -- examples from Liang Intro to Java Comprehensive 10e
public class AnalyzeNumbers {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the number of items: ");
int n = input.nextInt();
double[] numbers = new double[n];
double sum = 0;
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++) {
numbers[i] = input.nextDouble();
sum += numbers[i];
}
double average = sum / n;
int count = 0; // The number of elements above average
for (int i = 0; i < n; i++)
if (numbers[i] > average)
count++;
System.out.println("Average is " + average);
System.out.println("Number of elements above the average "
+ count);
}
}
public class CountLettersInArray {
/** Main method */
public static void main(String args[]) {
// Declare and create an array
char[] chars = createArray();
// Display the array
System.out.println("The lowercase letters are:");
displayArray(chars);
// Count the occurrences of each letter
int[] counts = countLetters(chars);
// Display counts
System.out.println();
System.out.println("The occurrences of each letter are:");
displayCounts(counts);
}
/** Create an array of characters */
public static char[] createArray() {
// Declare an array of characters and create it
char[] chars = new char[100];
// Create lowercase letters randomly and assign
// them to the array
for (int i = 0; i < chars.length; i++)
chars[i] = RandomCharacter.getRandomLowerCaseLetter();
// Return the array
return chars;
}
/** Display the array of characters */
public static void displayArray(char[] chars) {
// Display the characters in the array 20 on each line
for (int i = 0; i < chars.length; i++) {
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
}
/** Count the occurrences of each letter */
public static int[] countLetters(char[] chars) {
// Declare and create an array of 26 int
int[] counts = new int[26];
// For each lowercase letter in the array, count it
for (int i = 0; i < chars.length; i++)
counts[chars[i] - 'a']++;
return counts;
}
/** Display counts */
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++) {
if ((i + 1) % 10 == 0)
System.out.println(counts[i] + " " + (char)(i + 'a'));
else
System.out.print(counts[i] + " " + (char)(i + 'a') + " ");
}
}
}
public class DeckOfCards {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
// Initialize cards
for (int i = 0; i < deck.length; i++)
deck[i] = i;
// Shuffle the cards
for (int i = 0; i < deck.length; i++) {
// Generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
// Display the first four cards
for (int i = 0; i < 4; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println("Card number " + deck[i] + ": "
+ rank + " of " + suit);
}
}
}
public class AnalyzeNumbers {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the number of items: ");
int n = input.nextInt();
double[] numbers = new double[n];
double sum = 0;
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++) {
numbers[i] = input.nextDouble();
sum += numbers[i];
}
double average = sum / n;
int count = 0; // The number of elements above average
for (int i = 0; i < n; i++)
if (numbers[i] > average)
count++;
System.out.println("Average is " + average);
System.out.println("Number of elements above the average "
+ count);
}
}
import java.util.Scanner;
public class LottoNumbers {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
boolean[] isCovered = new boolean[99]; // default false
// Read all numbers and mark corresponding element covered
int number = input.nextInt();
while (number != 0) {
isCovered[number - 1] = true;
number = input.nextInt();
}
// Check if all covered
boolean allCovered = true; // Assume all covered
for (int i = 0; i < 99; i++)
if (!isCovered[i]) {
allCovered = false; // Find one number is not covered
break;
}
// Display result
if (allCovered)
System.out.println("The tickets cover all numbers");
else
System.out.println("The tickets don�t cover all numbers");
}
}
1 3 87 62 30 90 10 21 46 27
12 40 83 9 39 88 95 59 20 37
80 40 87 67 31 90 11 24 56 77
11 48 51 42 8 74 1 41 36 53
52 82 16 72 19 70 44 56 29 33
54 64 99 14 23 22 94 79 55 2
60 86 34 4 31 63 84 89 7 78
43 93 97 45 25 38 28 26 85 49
47 65 57 67 73 69 32 71 24 66
92 98 96 77 6 75 17 61 58 13
35 81 18 15 5 68 91 50 76
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment