i am new to java and i was trying to solve a program,
a) i wanted to print marks scored by 3 students in 3 subjects.
b) i wanted to get the total marks scored by each student and find out the highest total marks scored.
c)I also want to find the highest mark in each subject and by whom.
i have done a coding i was able to get a) done and b) partly done,can anyone help me with the coding so i can complete the problem.
<strong>class Exam1
{
public static void main(String args[])
{
int [][]subject= {{1,50,66,55},{2,40,42,48},{3,70,90,68}};
// display subject array//
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
System.out.print(subject[i][j] +"\t");
}
System.out.println( " ");
}
//to print total marks//
for(int i=0;i<3;i++)
{
int sum=0;
for(int j=1;j<4;j++)
{
sum= sum+ subject[i][j];
}
System.out.println("total marks of candidate "+ subject[i][0] + "=" + sum);
}
}
}</strong>
to find the max of three i would use Math.max(double,double) to find the max of two numbers then do another to find the max of the third number and the max of the first two.
i wrote this method that will take any amount of doubles separated by commas and return the max of all of them.
public static double maxMany(double... doubles){
if(doubles.length<=0){
throw new IllegalArgumentException();
}
double m = Double.MIN_VALUE;
for(int i=0;i<doubles.length;i++){
m=Math.max(m,doubles[i]);
}
return m;
}
cool, i wouldn't have tried thought to do this with objects, i have never written a compare to() method myself.
doesn't using generics mean that you cannot use primitive data types (int, double, long, short, byte, float, char, and boolean)? won't you have to create instances of the wrapper type first?
public static void main(String... args)
not many people know that you can declare main like this, or at least not many people do, good use of it considering the context of the rest of the code
cool, i wouldn't have tried thought to do this with objects, i have never written a compare to() method myself.
doesn't using generics mean that you cannot use primitive data types (int, double, long, short, byte, float, char, and boolean)? won't you have to create instances of the wrapper type first?
not many people know that you can declare main like this, or at least not many people do, good use of it considering the context of the rest of the code
If you look carefully at the code, the integers and doubles are used without directly creating objects of their type. The automatic boxing is done on the fly.
But I do believe there are primitive types without a wrapper class, and you would be correct. I would probably make a maxMany method that was specialized for those classes, in that case.
Edit: The code needs a minor edit. Instead of t being null it should initially be the first argument.
public class Exam3
{
public static void main(String args[])
{
int[][] subject = { { 1, 50, 66, 55 }, { 2, 40, 42, 78 }, { 3, 70, 92, 68 }, { 4, 90, 42, 44 }, { 5, 43, 45, 41 } };
// display subject array//
for (int i = 0; i < subject.length; i++)
{
for (int j = 0; j < subject[i].length; j++)
{
System.out.print(subject[i][j] + "\t");
}
System.out.println(" ");
}
//to print total marks//
for (int i = 0; i < subject.length; i++)
{
int sum = 0;
for (int j = 0; j < subject[i].length; j++)
{
if(j!=0) {
sum = sum + subject[i][j];
}
}
System.out.println("total marks of candidate " + subject[i][0] + "=" + sum);
}
//To Find the Maximum,.
//Initialize a variable to find the High Total.
int maxMarks = 0;
int studentNumber = 0;
int []_subjectArray = new int[(subject[0].length -1)];
int []_studentArray = new int[(subject[0].length -1)];
for(int i=0; i < subject.length; i++)
{
int sum = 0;
for(int j=0; j<subject[i].length; j++)
{
if(j!=0)
{
sum = sum + subject[i][j];
if(_subjectArray[(j-1)] < subject[i][j])
{
_subjectArray[(j-1)] = subject[i][j];
_studentArray[(j-1)] = (i+1);
}
}
}
//Find the Max Total.
if(maxMarks < sum)
{
//Then Make the Sum as the Maximum at any given time.
maxMarks = sum;
studentNumber = (i+1);
}
}//End of Number of Rows.
System.out.println("Maximum Marks : " + maxMarks + " And the Student Number : "+studentNumber);
for(int i=0; i<_subjectArray.length; i++)
{
System.out.println("Max in a Subject # " + (i+1) + " is =" + _subjectArray[i] + " And the Student is : "+_studentArray[i]);
}
}
}