Chapter 5: Computer Science (Java)
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of strings and two int variables, count and longest, write the code necessary to examine all the strings in the input source and determine how long the longest string (or strings are). That value should be assigned to longest.The number of strings that are of that length should be assigned to count.
count = 0; longest = 0; while (input.hasNext()) { String s = input.next(); if (s.length() > longest) { longest = s.length(); count = 1; } else if (s.length() == longest) count++; }
Given an int variable n that has already been declared and initialized to a positive value,use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.
do { System.out.print('*'); } while (--n > 0); System.out.println();
Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. The combinations should be displayed in ascending alphabetical order: aa ab ac ad ae ba bb ... ee
for (char i = 'a'; i <= 'e'; i++) for (char j = 'a'; j <= 'e'; j++) { System.out.print(i); System.out.println(j); }
Assume that the int variables i and j have been declared, and that n has been declared and initialized. Using for loops (you may need more than one), write code that will cause a triangle of asterisks of size n to be output to the screen. For example, if n = 3 , the triangle is as follows: * ** ***
for (i = 1; i <= n; i++) { for(j = 1; j <= i; j++) System.out.print('*'); System.out.println(); }
Write a for loop that prints in ascending order all the positive integers less than 200 that are divisible by both 2 and 3, separated by exactly one space.
for (int i = 1; i < 200; i++) { if (i % 2 == 0 && i % 3 == 0) { System.out.print(i + " "); } }
Write a for loop that prints in ascending order all the positive multiples of 5 that are less than 175, separated by exactly one space.
for (int n = 5; n < 175; n = n + 5) System.out.print(n + " ");
Write a for loop that prints all the even integers from 80 through 20 inclusive, separated by exactly one space.
for (int n = 80; n >= 20; n = n - 2) System.out.print(n + " ");
(Find the two highest scores) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score and the student with the second-highest score. Use the next() method in the Scanner class to read a name rather than using the nextLine() method. Assume that the number of students is at least 2. Sample Run Enter the number of students: 5 Enter a student name: Smith Enter a student score: 60 Enter a student name: Jones Enter a student score: 96 Enter a student name: Peterson Enter a student score: 85 Enter a student name: Greenlaw Enter a student score: 98 Enter a student name: Zhang Enter a student score: 95 Top two students: Greenlaw's score is 98.0 Jones's score is 96.0 Class Name: Exercise05_09
import java.util.*; public class Exercise05_09 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter the number of students System.out.print("Enter the number of students: "); int numberOfStudents = input.nextInt(); System.out.print("Enter a student name: "); String student1 = input.next(); System.out.print("Enter a student score: "); double score1 = input.nextDouble(); System.out.print("Enter a student name: "); String student2 = input.next(); System.out.print("Enter a student score: "); double score2 = input.nextDouble(); // Make sure that student1 is for the highest // and student2 is for the second highest if (score1 < score2) { // Swap String tempString = student1; double tempScore = score1; student1 = student2; score1 = score2; student2 = tempString; score2 = tempScore; } for (int i = 0; i < numberOfStudents - 2; i++) { System.out.print("Enter a student name: "); String student = input.next(); System.out.print("Enter a student score: "); double score = input.nextDouble(); if (score > score1) { student2 = student1; // student1 now is the second highest score2 = score1; student1 = student; // new student becomes the highest score1 = score; } else if (score > score2) { student2 = student; // new student becomes the second highest score2 = score; } } System.out.println("Top two students:"); System.out.println(student1 + "'s score is " + score1); System.out.println(student2 + "'s score is " + score2); } }
(Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. Sample Run 1 Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is 3 The number of negatives is 1 The total is 5.0 The average is 1.25 Sample Run 2 Enter an integer, the input ends if it is 0: 0 No numbers are entered except 0 Sample Run 3 Enter an integer, the input ends if it is 0: 2 3 4 5 0The number of positives is 4The number of negatives is 0The total is 14The average is 3.5 Sample Run 4 Enter an integer, the input ends if it is 0: -4 3 2 -1 0The number of positives is 2The number of negatives is 2The total is 0The average is 0.0 Class Name: Exercise05_01
import java.util.Scanner; public class Exercise05_01 { public static void main(String[] args) { int countPositive = 0, countNegative = 0; int count = 0, total = 0, number; Scanner input = new Scanner(System.in); System.out.print("Enter an integer, the input ends if it is 0: "); number = input.nextInt(); while (number != 0) { if (number > 0) countPositive++; else if (number < 0) countNegative++; total += number; count++; // Read the next number number = input.nextInt(); } if (count == 0) System.out.println("No numbers are entered except 0"); else { System.out.println("The number of positives is " + countPositive); System.out.println("The number of negatives is " + countNegative); System.out.println("The total is " + total); System.out.println("The average is " + total * 1.0 / count); } } }
Consider this data sequence: "3 11 5 5 5 2 4 6 6 7 3 -8". Any value that is the same as the immediately preceding value is considered a consecutive duplicate. In this example, there are three such consecutive duplicates: the 2nd and 3rd 5s and the second 6. Note that the last 3 is not a consecutive duplicate because it was preceded by a 7. Write some code that uses a loop to read such a sequence of non-negative integers, terminated by a negative number. When the code finishes executing, the number of consecutive duplicates encountered is printed. In this case, 3 would be printed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int n, prev, consecdups = 0; prev = stdin.nextInt(); n = stdin.nextInt(); while (n >= 0) { if (n == prev) consecdups++; prev = n; n = stdin.nextInt(); } System.out.println(consecdups);
Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a line by itself and separated by spaces, the sum of all the even integers read, the sum of all the odd integers read, a count of the number of even integers read, and a count of the number of odd integers read, all separated by at least one space. Declare any variables that are needed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int x; int esum = 0, ecount = 0; int osum = 0, ocount = 0; x = stdin.nextInt(); while (x > 0) { if (x % 2 == 0) { esum += x; ecount++; } else { osum += x; ocount++; } x = stdin.nextInt(); } System.out.println(esum + " " + osum + " " + ecount + " " + ocount);
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each followed by a space, and that terminates when it reads an integer that is not positive. Declare any variables that are needed. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
int x; x = stdin.nextInt(); while (x > 0) { if (x > 100) System.out.print(x + " "); x = stdin.nextInt(); }
Given an int variable k that has already been declared, use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.
k = 0; do { System.out.print('*'); k++; } while(k < 53); System.out.println();
Given an int variable k that has already been declared, use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.
k=1; while (k <= 88) { System.out.print('*'); k++; } System.out.println();
Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered. Assume the availability of a variable, stdin, that references a Scanner object associated with standard input. That is, stdin = new Scanner(System.in); is given.
n = -1; while (n<1 || n>10) n = stdin.nextInt();
Given int variables k and total that have already been declared, use a for loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total.Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0; for (k = 1; k <= 50; k++) total += k * k;
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
total = 0; for (k = 1; k <= n; k++) total += k * k * k;
Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0; k = 50; do { total += k * k; k--; } while (k > 0);
Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total.
total = 0; k = n; do { total += k * k * k; k--; } while (k > 0);
Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.
total = 0; k=50; while (k > 0) { total += k*k; k--; }
Given a Scanner reference variable named input that has been associated with an input source consisting of a sequence of lines, write the code necessary to read in every line and print them all out on a single line, separated by a space. Hint: Use input.nextLine() to read a line and use input.hasNextLine() to test if the input has a next line available to read.
while (input.hasNextLine()) { System.out.print(input.nextLine() + " "); }
Given an int variable n that has already been declared and initialized to a positive value, use a while loop to print a single line consisting of n asterisks. Use no variables other than n.
while (n > 0) { System.out.print('*'); n--; } System.out.println();