Professional Documents
Culture Documents
Scanner;
public class LetterGrades
{ public static void main(String[] args)
{
int total = 0; // sum of grades
int gradeCounter = 0; // number of grades entered
int aCount = 0; // count of A grades
int bCount = 0; // count of B grades
int cCount = 0; // count of C grades
int dCount = 0; // count of D grades
int fCount = 0; // count of F grades
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n %s%n %s%n",
"Enter the integer grades
in the range 0-100.",
"Type the end-of-file indicator to terminate input:",
"On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter",
"On Windows type <Ctrl> z then press Enter");
while (input.hasNext() )
{
int grade = input.nextInt();
total += grade;
++gradeCounter;
switch (grade / 10)
{
case 9:
case 10:
++aCount;
break;
case 8:
++bCount;
break;
case 7:
++cCount;
break;
case 6:
++dCount;
break;
default:
++fCount;
break;
}
}
System.out.printf("%nGrade Report:%n");
if (gradeCounter != 0)
{
double average = (double) total / gradeCounter;
System.out.printf("Total of the %d grades entered is %d%n",
gradeCounter, total);
System.out.printf("Class average is %.2f%n", average);
System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n",
"Number of students who received each grade:",
"A: ", aCount,
"B: ", bCount,
"C: ", cCount,
"D: ", dCount,
"F: ", fCount);
}
else
System.out.println("No grades were entered");
}
}
from one another by three spaces each. For example, if the user types
in the number 42339, the program should print
Assume that the user enters the correct number of digits. What
happens when you enter a number with more than five digits? What
happens when you enter a number with fewer than five digits? [Hint:
Its possible to do this exercise with the techniques you learned in
this chapter. Youll need to use both division and remainder
operations to pick off each digit.]
2.31 (Table of Squares and Cubes) Using only the programming
techniques you learned in this chapter, write an application that
calculates the squares and cubes of the numbers from 0 to 10 and
prints the resulting values in table format, as shown below.
2.32 (Negative, Positive and Zero Values) Write a program that inputs
five numbers and determines and prints the number of negative
numbers input, the number of positive numbers input and the number
of zeros input.
Making a Difference
2.33 (Body Mass Index Calculator) We introduced the body mass
index (BMI) calculator in Exercise 1.10. The formulas for calculating
BMI are
or
Create a BMI calculator that reads the users weight in pounds and
height in inches (or, if you prefer, the users weight in kilograms and
height in meters), then calculates and displays the users body mass
index. Also, display the following information from the Department of
Health and Human Services/National Institutes of Health so the user
can evaluate his/her BMI:
BMI VALUES Underweight: less than 18.5 Normal:
and 24.9 Overweight: between 25 and 29.9 Obese:
between 18.5
30 or greater
Here is a Java program to find Armstrong number, you can use same logic to write
code in any other programming language like C and C++.
29) Write a program to find all prime number up to a given numbers? (solution)
Here is another Java program to find prime numbers and print them. By using logic
demonstrated in this program; you can write similar program in C and C++.
30) Write function to compute Nth Fibonacci number? Both iterative and recursive?
(solution)
Check this Java program to print Fibonacci Series using recursion and iteration.
31) How to check if a number is binary? (solution)
For this question, you need to write a function which will accept an integer and
return true if it contains only 0 and 1 e.g. if input is 123 then your function will
return false, for 101 it should return true.
32) How to reverse an integer in Java? (solution)
33) How to count number of set bits in given integer? (solution)
34) How to find sum of digits of a number using recursion? (solution)
35) How to swap two numbers without using temp variable? (solution)
36) How to find largest of three integers in Java? (solution)
37) Write a program to find prime factors of integer? (solution)
38) How to add two integer without using arithmetic operator? (solution)
31) Write a program to find out if two rectangles R1 and R2 are overlapping?
32) You need to write a function to climb n steps you can climb either 1 step at a time
or 2 steps a time, write a function to return number of ways to climb a ladder with n
step.
33) Write code for Generate Random No in a range from min to max?
34) Write program for word-wrap which should work on any screen size?
35) Design an algorithm to find the frequency of occurrence of a word in an article?
36) Write a program to implement blocking queue in Java?
37) Write a program for producer-consumer problem? (solution)
This article solves producer consumer problem using BlockingQueue in Java. You can
refer it to answer this question.