• Forum searching still not well functioning . We have already working on it.

Soalan Java Programming

Recommended Brokers


Latest Threads

TyCo86

Fun Poster
Joined
Sep 19, 2013
Messages
227
Reaction score
0
Points
0
Assalammualaikum kepada member2 CG yang expert dlm Java. Boleh berikan bantuan untuk jawab soalan Java di bawah nie?:((

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

Note: This program can be written in a structured way.
 
Sponsored Post

kickdafella

Fun Poster
Joined
Oct 11, 2010
Messages
472
Reaction score
5
Points
0
package javaexercises.arrays;

import java.util.Scanner;


public class GradesAverage {

private final int LOWEST_GRADE = 0;
private final int HIGHEST_GRADE = 100;

// student's grades
private int[] grades;

private Scanner in;

/**
* Enter program's point.
*
* @param args
*/
public static void main(String[] args)
{
GradesAverage aGradesAverage = new GradesAverage();
aGradesAverage.in = new Scanner(System.in);

System.out.print("Enter the number of students: ");
int numStudents = aGradesAverage.in.nextInt();

aGradesAverage.run(numStudents);
**

/**
* Run program.
*
* @param numStudents
*/
private void run(int numStudents)
{
if (numStudents <= 0) {
System.out.println("Invalid number of students.");
return;
**
grades = new int[numStudents];

double sum = 0;
int i = 0;
while (i < numStudents)
{
System.out.printf("Enter the grade for student %1$d: ", (i+1));
int grade = in.nextInt();

// chek if grade is between 0 and 100
if ((grade >= LOWEST_GRADE) && (grade <= HIGHEST_GRADE)) {
grades = grade;
sum += grade;
i++;
continue;
**
System.out.println("Invalid grade, try again...");
**
System.out.printf("The average is %1$.2f\n", (sum / numStudents));
**

**
 

hackerism_11

Fun Poster
Joined
Mar 11, 2013
Messages
535
Reaction score
24
Points
18
package javaexercises.arrays;

import java.util.Scanner;


public class GradesAverage {

private final int LOWEST_GRADE = 0;
private final int HIGHEST_GRADE = 100;

// student's grades
private int[] grades;

private Scanner in;

/**
* Enter program's point.
*
* @param args
*/
public static void main(String[] args)
{
GradesAverage aGradesAverage = new GradesAverage();
aGradesAverage.in = new Scanner(System.in);

System.out.print("Enter the number of students: ");
int numStudents = aGradesAverage.in.nextInt();

aGradesAverage.run(numStudents);
**

/**
* Run program.
*
* @param numStudents
*/
private void run(int numStudents)
{
if (numStudents <= 0) {
System.out.println("Invalid number of students.");
return;
**
grades = new int[numStudents];

double sum = 0;
int i = 0;
while (i < numStudents)
{
System.out.printf("Enter the grade for student %1$d: ", (i+1));
int grade = in.nextInt();

// chek if grade is between 0 and 100
if ((grade >= LOWEST_GRADE) && (grade <= HIGHEST_GRADE)) {
grades = grade;
sum += grade;
i++;
continue;
**
System.out.println("Invalid grade, try again...");
**
System.out.printf("The average is %1$.2f\n", (sum / numStudents));
**

**


wah.. power2..
slalu java ni pakai software ape ? yang den tau dev c++
 

TyCo86

Fun Poster
Joined
Sep 19, 2013
Messages
227
Reaction score
0
Points
0
package javaexercises.arrays;

import java.util.Scanner;


public class GradesAverage {

private final int LOWEST_GRADE = 0;
private final int HIGHEST_GRADE = 100;

// student's grades
private int[] grades;

private Scanner in;

/**
* Enter program's point.
*
* @param args
*/
public static void main(String[] args)
{
GradesAverage aGradesAverage = new GradesAverage();
aGradesAverage.in = new Scanner(System.in);

System.out.print("Enter the number of students: ");
int numStudents = aGradesAverage.in.nextInt();

aGradesAverage.run(numStudents);
**

/**
* Run program.
*
* @param numStudents
*/
private void run(int numStudents)
{
if (numStudents <= 0) {
System.out.println("Invalid number of students.");
return;
**
grades = new int[numStudents];

double sum = 0;
int i = 0;
while (i < numStudents)
{
System.out.printf("Enter the grade for student %1$d: ", (i+1));
int grade = in.nextInt();

// chek if grade is between 0 and 100
if ((grade >= LOWEST_GRADE) && (grade <= HIGHEST_GRADE)) {
grades = grade;
sum += grade;
i++;
continue;
**
System.out.println("Invalid grade, try again...");
**
System.out.printf("The average is %1$.2f\n", (sum / numStudents));
**

**
ambil dri cni https://github.com/java-tester-x/javaexercises/blob/master/arrays/GradesAverage.java ka bro?dah try code nie tpi ada error..
 

Vpoint7

Fun Poster
Joined
Feb 11, 2009
Messages
230
Reaction score
18
Points
18
Assalammualaikum kepada member2 CG yang expert dlm Java. Boleh berikan bantuan untuk jawab soalan Java di bawah nie?:((

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

Note: This program can be written in a structured way.

kickdafella said:
ambil dri cni https://github.com/java-tester-x/jav...esAverage.java ka bro?dah try code nie tpi ada error..
Soalan ni dah dpt jawapan ke ?

Hmm... sy cuba jawabkan soalan ini wlaupn tak pro, sambil guna code yg dikatakan error tu..:-? Sbnrnya soalan TyCo86 tak boleh pakai direct dgn jwpn kickdafella dari github tu, sbb asal soalan adalah berbeza, jwpn mesti lah lain kan ?

Tapi kalau nak cuba jugak guna jwpn tu, jom kita kasi buat smpi jadi utk soalan tn TyCo86. Saya cuba kan... kwn2 lain, jika ada cara masing2, bantu lah tn TyCo86..
Ada lagi cara lain, cuma sy gunakan konsep serupa, tn TyCo86 mgkn dah cuba tgk code github tu sblm ni, kali ini tgk sekali lagi & lihat perbezaan dgn kesesuaian soalan...

Code dari github itu xde error sbnrnya, hanya perlu betulkan curly braces sahaja.. baru boleh run dgn baik...

Saya buatkan video tutorial utk ini, senang sikit nak faham konsep, dan nmpk laluan nya, klu terus letak copy-paste code, risau ade missing bracket..

Mohon rujuk disini, sy letak di domain website peribadi.. harap membantu...
Code:
http://warisanbernama.com/video/JavaTutorial.mp4
Rujuk video utk perbandingan sblm nak copy-paste code -> Gantikan simbol ** kpd yg sepatutnya :)
Code:
/* 
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

1. Java called Marks
2. Read from keyboard & saves in INT - variable called numOfMarks
3. Program should check marks between 0-100, quantity of marks, also between 1-30.
4. Display marks more than 50.

*/

import java.util.Scanner;
public class Marks {

	/* kalau tidak guna bahagian ini pun boleh, ditukar dgn menggunakan angka */
	private final int LOW_MARKS = 0;
	private final int HIGH_MARKS = 100;
	private final int MEDIUM_MARKS = 50;
	
	private int[] marks;
	private Scanner in;

	public static void main(String[] args) {
	
	Marks aMarks = new Marks();
	aMarks.in = new Scanner(System.in);
	
	System.out.print("Enter the number of marks: ");
	int numOfMarks = aMarks.in.nextInt();
	
	aMarks.run(numOfMarks);	
	**	
	private void run(int numOfMarks) {	
		if (numOfMarks < 1 || numOfMarks > 30) {
		System.out.println("Invalid number of marks, please enter between 1-30 only");
		return;		
		**		
		marks = new int[numOfMarks];
		int a = 0;
		int i = 0;					
		while (i < numOfMarks) {
			System.out.printf("Enter marks %1$d: ", (i+1));
			int marks = in.nextInt();

			//kuantiti markah lebih drpd 50
			if (marks >= MEDIUM_MARKS && marks <= HIGH_MARKS) {
				a++; // a = a + 1	
			**

			//markah mesti dlm lingkungan 1-100 sahaja	
			if ((marks > LOW_MARKS) && (marks <= HIGH_MARKS)) {
			        i++;
			        continue;					
			**			
			System.out.println("Invalid marks.. try again...");						
		**
		System.out.println("Number of marks more than 50 is "+ a);
	**
**
 
Last edited:

tomi

Active Member
Joined
Mar 10, 2015
Messages
1,791
Reaction score
409
Points
0
Assalammualaikum kepada member2 CG yang expert dlm Java. Boleh berikan bantuan untuk jawab soalan Java di bawah nie?:((

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

Note: This program can be written in a structured way.
seeloknya belajar coding step by step.

pecahkan setiap masalah dan cuba code untuk setiap satu, kalau terus nak buat complete code memang rasa susah.
 

Vpoint7

Fun Poster
Joined
Feb 11, 2009
Messages
230
Reaction score
18
Points
18
Setuju dgn nasihat tomi, sy harap jwpn di atas boleh run tanpa sebarang error... :)
 
Sponsored Post
Top
Log in Register