The Artima Developer Community
Sponsored Link

Java Answers Forum
Confused and need help desperately

6 replies on 1 page. Most recent reply: Nov 23, 2002 8:32 AM by Joyce Ann

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Chris Jericho

Posts: 1
Nickname: jericho
Registered: Nov, 2002

Confused and need help desperately Posted: Nov 20, 2002 1:45 AM
Reply to this message Reply
Advertisement
could someone help me, i need a program that should ask the user to enter the total number of students in a class. this should be no greater than 20. if the user enters 0, the program should terminate. if the number is less than 0 or greater than 20, an error message should be displayed and the user should be asked to enter another number. for all other numbers (1 to 20), the user will be asked to enter a mark out of 100 for each student. for entires less than 0 or greater than 100, an error message should be displayed and repaet entry requested. after a mark has been entered, a calculation should be done to see what category it falls in and a count of the total number of each category should be kept. Categories are:
First Class (70 to 100)
Upper Second (60 to 69)
Lower Second (50 to 59)
Third (41 to 49)
Pass (38 to 40)
Fail (0 to 37)
when all of the marks have been entered, an output box should be displayed showing various statistics, namely:
Number of marks in each category and the percentage that this represents
The Average mark (correct to two decimal places)
The highest mark
the lowest mark
Class size

This is what i got to do, would anyone be able to help me by showing coding for this as i am only a beginner. thank you


twisty

Posts: 22
Nickname: twisty
Registered: Nov, 2002

Re: Confused and need help desperately Posted: Nov 20, 2002 4:42 AM
Reply to this message Reply
Hi Chris. Heres a bit of code to get you up and running:

import javax.swing.JOptionPane;
 
public class StudentResults {
	private int totalStudents, result;
	
	/** Constructor. */
	public StudentResults() {
		totalStudents = Integer.parseInt( JOptionPane.showInputDialog( 
			"What is the total number of students? \n [ Enter 0 to exit ]" ) );
			
		while( totalStudents > 20 || totalStudents < 0 ) {
			totalStudents = Integer.parseInt( JOptionPane.showInputDialog( 
			"Invalid amount! Please enter a value between 1 & 20. \n" +
			"[ Enter 0 to exit ]" ) );	
		}
		
		if( totalStudents == 0 ) 
			exitProgram();		
 
		Student students[] = new Student[totalStudents];		
		
		// Enter in results
		for( int i=0; i < students.length; i++ ) {
			result = Integer.parseInt( JOptionPane.showInputDialog( 
					"Please enter a mark for student " + (i+1) + ":" ) );			
			
			while( result < 0 || result > 100 ) {
				result = Integer.parseInt( JOptionPane.showInputDialog( 
					"Please enter a mark for student " + (i+1) + ":" ) );	
			}
			students[i] = new Student();
			students[i].setResult(result);
			students[i].setAward( 
				resultCategory( students[i].getResult() ) );
		}
		
		////// <TESTING>
		for( int i=0; i < students.length; i++ ) {
			System.out.println( "Student " + (i+1) + " got " + 
				students[i].getResult() + " and got a " + 
				students[i].getAward() + " result." );
		}
		////// </TESTING>
		
		exitProgram();	
	}
	
	/** Evaluates a result and returns a category. */
	public String resultCategory( int result ) {
		String category = "";
		
		if( ( result <= 100 && result >= 70 ) )
			category = "First Class";
		else if( ( result <= 69 && result >= 60 ) )		
			category = "Upper Second";
		else if( ( result <= 59 && result >= 50 ) )		
			category = "Lower Second";
		else if( ( result <= 49 && result >= 41 ) )		
			category = "Third";
		else if( ( result <= 40 && result >= 38 ) )		
			category = "Pass";
		else
			category = "Fail";
		
		return category;	
	}
 
	/** Allows program to exit. */
	public void exitProgram() {
		System.exit(0);	
	}	
	
	/** Main. */
	public static void main( String args[] ) {
		StudentResults sr = new StudentResults();		
	}	
	
	/** Inner class Student. Should really be made as a separate class. */
	private class Student {
		private String 	award;
		private int		result;
		
		public String 	getAward() { return award; }
		public int 		getResult()	{ return result; }
		
		public void		setAward( String s ) { this.award = s; }
		public void		setResult( int i ) { this.result = i; }	
	}
	
}


I don't want to code it all up for you in one go, as I think that you would benefit from having a go at doing some of those functions yourself. However, if your having difficulty doing that, let me know and I will be happy to help.

twisty

Posts: 22
Nickname: twisty
Registered: Nov, 2002

Re: Confused and need help desperately Posted: Nov 20, 2002 4:46 AM
Reply to this message Reply
You will notice that the &gt; above has been replaced with the html code instead. This is an error in the way the forum code parses text in between the JAVA tags. You will need to replace those in your code.

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Confused and need help desperately Posted: Nov 22, 2002 12:44 AM
Reply to this message Reply
> You will notice that the > above has been
> replaced with the html code instead. This is an error in
> the way the forum code parses text in between the JAVA
> tags. You will need to replace those in your code.

Not anymore. I just fixed that bug in the forums. The problem was in the filter that does syntax highlighting of Java code on its way out of the database, so past posts such as this one enjoy the benefit of the bug fix as well as new posts.

twisty

Posts: 22
Nickname: twisty
Registered: Nov, 2002

Re: Confused and need help desperately Posted: Nov 22, 2002 1:39 AM
Reply to this message Reply
Thanks :).

B.t.w. Are there any plans to allow editing of posts?

jaz

Posts: 4
Nickname: jaz
Registered: Oct, 2002

Confused and need help desperately Posted: Nov 22, 2002 6:52 AM
Reply to this message Reply
When the program starts, the Demo window will fill ? the area of the screen, and the center of the
window will be at the center of the screen. This sizing and positioning will be based on the current
screen display mode, not on hard-coded constants. The window title will be "Event-Handling
Demonstration". When the window is closed, the program will end.

The DemoPanel class will handle keyboard events using an inner class or classes. The
isFocusTraversable method will be used to allow the DemoPanel to obtain keyboard focus.
DemoPanel will collect from the keyboard all characters that are not action keys. Each time the Enter
key is pressed, DemoPanel will set the title of the Demo window equal to the characters collected since
the last time the Enter key was pressed, not including the Enter key. If the characters entered are an
integer number greater than 0, DemoPanel will set the current drawing square size (see below) equal to
this number. To set the square size, use a code structure similar to the following, where str is the
String of collected characters:
try {
squareSize = Integer.parseInt(str);
} catch (NumberFormatException xcp) {};

The DemoPanel class will use an inner class or classes to handle mouse events. Each time the mouse is
dragged with the left button depressed (not the right), the DemoPanel will set a filled square of pixels
at the mouse position to the current drawing color. This drawing will be done such that it is not
necessary to resize the window to get the drawing displayed. The center of the square will be at the
mouse position. The size of the square will be the current drawing size. The initial drawing size will be 2
pixels (a 2 X 2 square). Each time the left mouse button is double clicked, the current drawing color will
be set to the next color, in round-robin fashion. Changing the current drawing color will not change the
color of squares already drawn. The set of drawing colors will be blue, red, and green. The initial
drawing color will be blue. If the right mouse button is double clicked, the program will remove all the
squares drawn previously, so that they no longer exist. A crosshair cursor will be displayed whenever
the cursor is on the DemoPanel.

The Square class will have all fields required to define the position of a square, its size, and its color.
Square will have the following public method plus any other methods required:
public void draw(Graphics g), that draws the square.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DemoWindow extends JFrame {
private final int WIDTH = 200, HEIGHT = 200;
public DemoWindow() {
//set title
setTitle( "Event-Handling Demostration" );
//get screen size
Dimension d = this.getToolkit().getScreenSize();
//1. if u want the fram to fill the whole screen use this code
setLocation( 0, 0 );
//may not need because it's default
setSize( d.width, d.height );
//2. if u have a custom frame size use this code to
// dynamically center it (assume the given size at top)
setSize( WIDTH, HEIGHT );
setLocation( (d.width-this.getWidth()) /2,
(d.height-this.getHeight())/ 2 );
//to end program when frame is closed (using anynomus inner class)
addWindowListener( new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{ System.exit( 0 ); } });
//end of inner class }
public static void main( String[] args )
{ DemoWindow test = new DemoWindow(); test.setVisible( true ); }} //end of DemoWindow class


Joyce Ann

Posts: 5
Nickname: newtoforum
Registered: Nov, 2002

Re: Confused and need help desperately Posted: Nov 23, 2002 8:32 AM
Reply to this message Reply
here is my long solution:

import java.io.*;
import java.util.*;
import java.text.*;

public class Assignment { // begin Assignment
public static void main(String[] s) throws IOException { // begin main
boolean loopControl = true;
BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
int classSize = 0;
int highestMark = Integer.MIN_VALUE;
int lowestMark = Integer.MAX_VALUE;
double averageMark; int studentMark;
double firstClass = 0.0; double upperSecond = 0.0; double lowerSecond = 0.0;
double third = 0.0; double pass = 0.0; double fail = 0.0; double classMarkSum = 0.0;

while (loopControl) { // begin while
System.out.println("enter number of students : ");
classSize = Integer.parseInt(in.readLine());
if (classSize == 0)
System.exit(0);
if ((classSize >= 1) && (classSize <= 20))
loopControl = false;
} // end while
int counterControlLoop = classSize;
while(counterControlLoop > 0) { // begin while
System.out.println("Enter student mark: ");
studentMark = Integer.parseInt(in.readLine());
if ((studentMark >= 1) && (studentMark <= 100)) {
// get the highest and lowest mark
if (studentMark > highestMark) highestMark = studentMark;
if (studentMark < lowestMark)lowestMark = studentMark;
// get the category
if (studentMark >= 70) firstClass++;
else if (studentMark >= 60) upperSecond++;
else if (studentMark >= 50) lowerSecond++;
else if (studentMark >= 41) third++;
else if (studentMark >= 38) pass++;
else fail++;
// get the sum to be used in average mark
classMarkSum += studentMark;
counterControlLoop--;
} else
System.out.println("enter student mark in the range : 0 - 100");
} // end while
Locale currentLocale = new Locale("en", "US");
Double percent = new Double((firstClass/classSize));
NumberFormat percentFormatter = NumberFormat.getPercentInstance(currentLocale);
String percentOut = percentFormatter.format(percent);
System.out.println("Number of marks in each category and the percentage");
System.out.println("First Class (70 to 100) :" + firstClass + " : " + percentOut);
percent = new Double((upperSecond/classSize));
percentOut = percentFormatter.format(percent);
System.out.println("Upper Second (60 to 69) :" + upperSecond + " : " + percentOut);
percent = new Double((lowerSecond/classSize));
percentOut = percentFormatter.format(percent);
System.out.println("Lower Second (50 to 59) :" + lowerSecond + " : " + percentOut);
percent = new Double((third/classSize));
percentOut = percentFormatter.format(percent);
System.out.println("Third (41 to 49) :" + third + " : " + percentOut);
percent = new Double((pass/classSize));
percentOut = percentFormatter.format(percent);
System.out.println("Pass (38 to 40) :" + pass + " : " + percentOut);
percent = new Double((fail/classSize));
percentOut = percentFormatter.format(percent);
System.out.println("Fail (0 to 37) :" + fail + " : " + percentOut);
averageMark = classMarkSum / classSize;
DecimalFormat myDecimalFormatter = new DecimalFormat("###.##");
String average = myDecimalFormatter.format(averageMark);
System.out.println("The Average mark : " + average);
System.out.println("The highest mark : " + highestMark);
System.out.println("The lowest mark : " + lowestMark);
System.out.println("Class size : " + classSize);

} // end main
} // end Assignment

Flat View: This topic has 6 replies on 1 page
Topic: cannot access: package--->default Previous Topic   Next Topic Topic: How to use a Unicode simbols in Label or JButtons?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use