•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,015 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,660 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 478 | Replies: 8
![]() |
•
•
Join Date: Jul 2008
Posts: 18
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
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);
}
}
} Last edited by Tekmaven : Jul 28th, 2008 at 2:35 pm. Reason: Code tags
please use code tags.
to find the max of three i would use
i wrote this method that will take any amount of doubles separated by commas and return the max of all of them.
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;
} Last edited by sciwizeh : Jul 24th, 2008 at 4:50 pm.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
Here's a Generic extension of your max-value finder where anything that implements Comparable can be compared--
java Syntax (Toggle Plain Text)
public class Manip{ public static void main(String... args){ System.out.println(Manip.<Integer>maxMany(1, 5, 4, 9, 2)); System.out.println(Manip.<Double>maxMany(2.2, 1.7, 11.2, -7.8, 2.0)); System.out.println(Manip.<String>maxMany("Joe", "Bob", "Mark", "Luke", "John")); } public static <T extends Comparable<T> > T maxMany(T... args){ if(args.length<=0){ throw new IllegalArgumentException(); } T t = null; for(int i=0;i<args.length - 1;i++){ if(args[i].compareTo(args[i + 1]) > args[i + 1].compareTo(args[i])) t=args[i]; } return t; } }
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
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
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
•
•
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.
Last edited by Alex Edwards : Jul 24th, 2008 at 10:13 pm.
do they help?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
Join Date: Jul 2008
Posts: 18
Reputation:
Rep Power: 1
Solved Threads: 0
yes and i also tried a new coding which helped.
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]);
}
}
} Last edited by Tekmaven : Jul 28th, 2008 at 2:33 pm. Reason: Code tags
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- pointers and multi-dimensional arrays (C++)
- C++ Multi dimensional arrays (C++)
- Multi-dimensional Arrays: (Python)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- How to implement multi-dimensional arrays in Python?? (Python)
- Arrays (PHP)
- Need help passing a multi-dimensional array (C++)
Other Threads in the Java Forum
- Previous Thread: how to create smtp server java
- Next Thread: Need help with Java Payroll Program Part 2



Linear Mode