Created
October 30, 2018 04:56
-
-
Save burhanloey/536cb87b9ddfa7b297af721a6930189b to your computer and use it in GitHub Desktop.
Solution for question from https://carigold.com/portal/forums/showthread.php?t=681213
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* Solution for an exercise with the question: | |
* | |
* Write a program in Java called Marks, which prompts user for the number of marks, reads it from the keyboard, and | |
* saves it in an int variable called numOfMarks. It then prompts user for the marks and saves them in an int array | |
* called marks. Your program shall check that the marks value is between 0 and 100 and the number of marks is | |
* between 1 to 30. The program should display number of marks that are more than 50. An example of output is shown | |
* below: | |
* | |
* | |
* Enter the number of marks: 2 | |
* Enter marks 1: 112 | |
* Invalid marks.. try again... | |
* Enter marks 1: 49 | |
* Enter marks 2: 60 | |
* Number of marks more than 50 is 1 | |
*/ | |
public class Marks { | |
private final Scanner scanner; | |
/** | |
* Constructor. | |
*/ | |
public Marks() { | |
scanner = new Scanner(System.in); | |
} | |
/** | |
* Call this method to run the program. | |
*/ | |
public void run() { | |
int[] marks = promptNumOfMarks(); | |
for (int i = 0; i < marks.length; i++) { | |
marks[i] = promptMark(i); | |
} | |
int moreThanFifty = countMoreThanFifty(marks); | |
System.out.println("Number of marks more than 50 is " + moreThanFifty); | |
} | |
/** | |
* Prompt for number of marks. Number of marks must be between 1 and 30. | |
* | |
* @return Array with the size of number of marks | |
*/ | |
private int[] promptNumOfMarks() { | |
while (true) { | |
System.out.print("Enter the number of marks: "); | |
int numOfMarks = scanner.nextInt(); | |
if (numOfMarks >= 1 && numOfMarks <= 30) { | |
return new int[numOfMarks]; | |
} | |
System.out.println("Number of marks must be between 1 and 30."); | |
} | |
} | |
/** | |
* Prompt for mark given the index. Mark must be between 0 and 100. | |
* | |
* @param index The index of mark from the array | |
* @return Mark | |
*/ | |
private int promptMark(int index) { | |
int markPosition = index + 1; | |
while (true) { | |
System.out.print("Enter mark " + markPosition + ": "); | |
int mark = scanner.nextInt(); | |
if (mark >= 0 && mark <= 100) { | |
return mark; | |
} | |
System.out.println("Invalid marks. Try again."); | |
} | |
} | |
/** | |
* Count number of marks that are more than 50. | |
* | |
* @param marks Array of marks | |
* @return Number of marks that are more than 50 | |
*/ | |
private int countMoreThanFifty(int... marks) { | |
int count = 0; | |
for (int mark : marks) { | |
if (mark > 50) { | |
count++; | |
} | |
} | |
return count; | |
} | |
/** | |
* Program main entry. | |
*/ | |
public static void main(String[] args) { | |
new Marks().run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment