using class and methods

using class and methods

Sir,Plz help me to write this below pgm. here is the question:

Assume a table for annual examination results for 10 students. write a program to read the data and determine the following:

a) Total marks obtained by each students
b) The highest marks in each subject and roll no of the students who have secured it.
c) The student who obtained the highest total marks.Use appropriate classes and methods
View Answers

June 12, 2010 at 4:30 PM

Hi Friend,

Try the following code:

import java.util.*;

public class Student{
int rollNo;
double sub1;
double sub2;
double sub3;
double sub4;
double sub5;
double totalMarks;
public Student(int rollNo,double sub1,double sub2,double sub3,double sub4,double sub5,double totalMarks){
this.rollNo=rollNo;
this.sub1=sub1;
this.sub2=sub2;
this.sub3=sub3;
this.sub4=sub4;
this.sub5=sub5;
this.totalMarks=totalMarks;
}
public int getRollNo(){
return rollNo;
}
public double getSub1(){
return sub1;
}

public double getSub2(){
return sub2;
}

public double getSub3(){
return sub3;
}

public double getSub4(){
return sub4;
}

public double getSub5(){
return sub5;
}

public double getTotalMarks(){
return totalMarks;
}

public static void main(String[]args){
Scanner input=new Scanner(System.in);
double max1=0,max2=0,max3=0,max4=0,max5=0,max=0;
int no=0;
double s1=0,s2=0,s3=0,s4=0,s5=0,tm=0;
ArrayList<Student> list = new ArrayList<Student>();
for (int i=1; i<=2; i++) {
System.out.print("Enter Roll No: ");
no=input.nextInt();
System.out.print("Enter marks of Maths: ");
s1=input.nextDouble();
System.out.print("Enter marks of Science: ");
s2=input.nextDouble();
System.out.print("Enter marks of Hindi: ");
s3=input.nextDouble();
System.out.print("Enter marks of English: ");
s4=input.nextDouble();
System.out.print("Enter marks of Social Studies: ");
s5=input.nextDouble();
tm=s1+s2+s3+s4+s5;
list.add(new Student(no,s1,s2,s3,s4,s5,tm));
System.out.println();

if (s1 > max1) {
max1 = s1;
}
if (s2 > max2) {
max2 = s2;
}
if (s3 > max3) {
max3 = s3;
}
if (s4 > max4) {
max4 = s4;
}
if (s5 > max5) {
max5 = s5;
}
if (tm > max) {
max = tm;
}
}

June 12, 2010 at 4:31 PM

continue..

System.out.println("*********Roll No and Total Marks*********");
System.out.println("Roll No Total Marks");
for (Student s : list){
System.out.println(s.getSub1()+" "+s.getSub2());
}
System.out.println();
System.out.println("*********Highest marks in each subject with RollNO*********");
for (Student s : list){
if(s.getSub1()==max1){
System.out.println("Roll No " +s.getRollNo()+" get highest marks in Subject 1 i.e "+max1);
}
}
for (Student s : list){
if(s.getSub2()==max2){
System.out.println("Roll No " +s.getRollNo()+" get highest marks in Subject 2 i.e "+max2);
}
}
System.out.println();
for (Student s : list){
if(s.getSub3()==max3){
System.out.println("Roll No " +s.getRollNo()+" get highest marks in Subject 3 i.e "+max3);
}
}

for (Student s : list){
if(s.getSub4()==max4){
System.out.println("Roll No " +s.getRollNo()+" get highest marks in Subject 4 i.e "+max4);
}
}
for (Student s : list){
if(s.getSub5()==max5){
System.out.println("Roll No " +s.getRollNo()+" get highest marks in Subject 5 i.e "+max5);
}
}
System.out.println();
System.out.println("*********Student Obtained Highest Marks*********");
for (Student s : list){
if(s.getTotalMarks()==max){
System.out.println("Roll No " +s.getRollNo()+" get highest marks");
}
}

}
}

Thanks

June 12, 2010 at 4:34 PM

Hi Friend,

We have created a loop for only 2 students, you can place 10 in place of 2 so that program will display the results of 10 students.

Thanks









Related Tutorials/Questions & Answers:
using class and methods - Java Beginners
using class and methods  Sir,Plz help me to write this below pgm. here is the question: Assume a table for annual examination results for 10... the following code: import java.util.*; public class Student{ int rollNo
What's the proper way to test a class with private methods using JUnit?
What's the proper way to test a class with private methods using JUnit?  What's the proper way to test a class with private methods using JUnit
Advertisements
can we use scanner class,class , object and methods to get output without using constructor ????
can we use scanner class,class , object and methods to get output without using... am i getting error here... can we get output using scanner class , object and methods without using constructor??? //Program to illustrate the classes
to create a java class and methods
to create a java class and methods  How to create a java class without using the library class to represent linked lists of integers and also provide it with methods that can be used to reverse a list & append two lists.Also
java object class methods
java object class methods  What are the methods in Object class?  There are lots of methods in object class. the list of some methods are as- clone equals wait finalize getClass hashCode notify notifyAll
Getting Methods Information of a class
Getting Methods Information of a class   ... to retrieve information of all methods of a class (that included in the program... by using different methods on that method object. Here is the example code
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
what is class methods in objective c
what is class methods in objective c  What is class methods in objective c? Explain the class method of objective c with the help of an example
PHP list class methods
Function get_class_methods gives the all methods names of the given class. It takes input as class name and returns the array of the methods name PHP LIst Class Methods Example <?php class myclass{ function aa
Class AsyncEvent important methods
In this section, you will learn about the important methods of AsyncEvent Class
write a program to demonstrate wrapper class and its methods......
write a program to demonstrate wrapper class and its methods......  write a program to demonstrate wrapper class and its methods
variables and methods declared in abstract class is abstract or not
variables and methods declared in abstract class is abstract or not  variables and methods declared in abstract class is abstract
creating class and methods - Java Beginners
of the Computers. This class contains following methods, - Constructor method...creating class and methods  Create a class Computer that stores... ( ) method that creates array of 4 objects of Computer class and that takes input
Abstract class,Abstract methods and classes
or the base class. Subclasses are derived to implement the methods inherited from... Abstract methods and classes     ... is used with methods and classes.  Abstract MethodADS_TO_REPLACE_1
An application using swings and vector methods
An application using swings and vector methods   Hi, I want an application in Java swings which uses good selection of Vectors methods
Object Class Methods in Java
We are going to discus about Object Class Methods in Java... or indirectly. There are many methods defined in java.lang.Object class...(), wait(), etc Java object class methods are:- finalize() clone() equals
Abstract class or methods example-1
;Animal class"); // this     // ...;form Animal class");   } } class ...;called form BuzzwordAnimal class");   }  
Calling Methods Using SpEL
Calling Methods Using SpEL Spring 3 provides powerful Expression Language... by calling methods. Person.java: The Person class contains property named "... getPersonName() method to wire name property of the Person class using Spring
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I am trying to do is create a code that lists music artists names using user input. It has to have multiple classes so I will have a main class, and a class
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays  So what I am trying to do is create a code that lists music artists names using user input. It has to have multiple classes so I will have a main class, and a class
Can a abstract class be defined without any abstract methods?
Can a abstract class be defined without any abstract methods?   hi, Can a abstract class be defined without any abstract methods? thanks
code for multiplication of matrix in java using methods
code for multiplication of matrix in java using methods  code for multiplication of matrix in java using methods
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
methods
methods  PrintStream class has two formatting methods,what
Using Abstract Class
Using Abstract Class      ... class. This class must be inherited. Unlike interface the abstract class may implement some of the methods defined in the class, but in this class at least one
Calling Static Methods Using SpEL
property of the MyClass class using T() operator of Spring expression...Calling Static Method Using SpEL Spring 3 provides powerful Expression... method of any bean using SpEL's T() operator. Lets take an example
Can I have multiple main() methods in the same class?
Can I have multiple main() methods in the same class?  Hi, Can I have multiple main() methods in the same class?   Hi, No, We cann't use multiple main() method within the same class. If we do this then the program
Java Abstract Class Example
using 'abstract' keyword. An abstract class may contain abstract.... A subclass of an abstract class must have to define all of its abstract methods..._TO_REPLACE_5 An another way to create and call the methods of abstract class
Defining handler methods using @RequestMapping annotation
In this section, you will learn about defining new handler methods using @RequestMapping annotation
Call class methods
Call class methods       This Example shows you how to call class method in velocity... through method  init(). 2: Create object of VelocityContext Class. 3
Class, Object and Methods
Class, Object and Methods      ... methods. Constructor is the method which name is same to the class.... This program is using two classes. First class is another and second is the main
Java :Thread Methods
Java :Thread Methods This section explains methods of Thread class. Thread Methods : Thread class provides many method to handle different thread... are constructing thread by using a separate Runnable run object, then run method
Abstract class or methods example-2
;defined in the non abstract class } abstract class ...;runLion1(); // defined in the non abstract class
How to access sub class member using super class object
How to access sub class member using super class object  class A{ } class B extends A{ int b=1; } class Test{ public static void main(String args[]) { what to do here to access sub class variable b using super class object
core java ,io operation,calling methods using switch cases
core java ,io operation,calling methods using switch cases  How to create a dictionary program,providing user inputs using io operations with switch cases and providing different options for searching,editing,storing meanings
Java Abstract Class
; An abstract class is a class that is declared by using the abstract keyword... an abstract class. Which helps us to organize our classes based on common methods... for all of the abstract methods. The key idea with an abstract class is useful when
PHP Static Variable and Methods
then we can access the method using class, without instantiating any object. Example of PHP Static Variables & Methods : <?phpADS_TO_REPLACE_2 class...PHP Static Methods and Variables: In this tutorial we will study when we
Java - Class, Object and Methods in Java
Java - Class, Object and Methods in Java   ... will see how to use the class, object and it's methods. This program uses... the main function is declared. First class is square which is using for getting
Abstract class or methods example-3
abstract class BaseAbstractClass {   abstract void baseMethod1();    // Definition done in DerivedClass class   void 
Decompress file using Inflater class.
Decompress file using Inflater class. In this tutorial, we will define the use of Inflater class. The Inflater class provide support for Decompression using... Example, we will explain how to decompress file using Inflater class. The Inflater
How to add new class in Pkg using the feature of existing class - Java Beginners
How to add new class in Pkg using the feature of existing class   Hi Friends, I want to know , is it possible to add new class in a user Defined... access Variables or methods. Can i use them in another Class in the Same Pkg
How to Compress file using DeflaterOutputStram class in java.
How to Compress file using DeflaterOutputStram class in java. In this tutorial... will compressed the data of a file by using DeflaterOutputStream class... java.io.FilterIntputStream class. It provides the following methods:ADS_TO_REPLACE_1
how to use StringTokenizer to retrieve the class name, attributes name and methods name from the Java Source Code
one problem which is retrieve the Class Name, Attributes Name and Methods Names...how to use StringTokenizer to retrieve the class name, attributes name and methods name from the Java Source Code  hi, I have done a program whereby
Using a user defined non-packaged servlet class in jsp. - JSP-Servlet
Using a user defined non-packaged servlet class in jsp.  i want know that how to use a user defined java servlet function's, variables and methods... example. actually i have a class in which i had written the backend process
Given a list of methods for a stateful or stateless session bean class, define which of the following operations can be performed from each of those methods: SessionContext interface metho
Given a list of methods for a stateful or stateless session bean class... of those methods: SessionContext interface methods, UserTransaction methods, Java Naming and Directory Interface API (JNDI API) access to java
Access Static Member Of The Class Through Object
that work without any object of the class. Static methods are limited to calling other static methods in the class and to using only static variables... Access Static Member Of The Class Through Object  
Setting Fields Information of a class using Reflection
Setting Fields Information of a class using Reflection... we have seen that we can get field values of a class by using the Field class. Now we can also set different field values of that class by using set() method
String toLowerCase and toUppercase methods example
() methods example:- String class provides methods toUpperCase() and toLowerCase... to  lowercase. We have used these two methods in this example. user can see how to use these methods. Example:-ADS_TO_REPLACE_1 <?xml
Immutable Image using Canvas Class
Immutable Image using Canvas Class   ... to create the immutable image using canvas. In this example ImageCanvas class extends the Canvas class to create the immutable image. In the constructor

Ads