Logical error ?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Logical error ?

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    12

    Logical error ?

    Hello guys,
    I'm wrote 2 classes Student & ArrayStudent class program that determines the average, youngest and oldest age in an array....the average and oldest works fine, but the youngest keeps on giving wrong output in the ArrayStudent class, no matter what I try....Please can anybody help and tell me what I'm doing wrong?
    thanks for your anticipated help.
    Code:
    // Fig. 1.3: Student.java
    // Student class definition
    public class Student
    {
    	// A student's personal details
    	private String name;
    	private int age;
    
    	// Student constructor initialises each instance variable
    	public Student(String nameIn, int ageIn)
    	{
    		name = nameIn;
    		age = ageIn;
    	}
    
    	// Return a student's name
    	public String getName() {  return name; }
    
        // Return a student's age
        public int getAge()  { return age; }
    }// end of class Student
    Code:
    // Fig. 1.3: ArrayStudent.java
    // Application Class ArrayStudent is a testbed for class Student
    
    
    import java.io.*;
    public class ArrayStudent
    {
        // keyboard input
        static BufferedReader keyboard = new BufferedReader(
                                         new InputStreamReader(System.in));
    
        public static void main(String args[] )throws IOException
        {
            String name;
            int age, number;
            System.out.print("How many students? ");
            number = Integer.parseInt(keyboard.readLine());
            Student s[] = new Student[number];
            // input into the array
            for(int index = 0; index < number; index++)
            {
              System.out.println();
              System.out.print("Enter Name of student number " + (index+1) + "? ");
              name = keyboard.readLine();
              System.out.print("Enter Age for " + name + "? ");
              age = Integer.parseInt(keyboard.readLine());
              s[index] = new Student(name,age);
            }
            printArray(s);  
            
        }// end of main
        
        // Prints elements of array - "Student objects"
        private static void printArray(Student s[])
        {
            System.out.println("Student List:");
            for(int index = 0; index < s.length; index++)
            {
              System.out.println(s[index].getName() + " " + s[index].getAge());
            }
            System.out.println("The total average age is " + findAverage(s));
            System.out.println("The youngest age is " + youngestAge(s));
            System.out.println("The oldest age is " + oldestAge(s));
        }// end of method printArray
        
        // Finding the average age.
        private static double findAverage(Student s[])
        {
            double sum = 0.0;
            for(int i = 0; i < s.length; i++)
            {
               sum += s[i].getAge();
    
            }
            return sum / s.length;        
        }
        
        //Finding the youngest age.
        private static int youngestAge(Student s[])
        {
            int youngest = 0;
            //youngest = s.[0];
            for(int i = 0; i < s.length; i++){
            
            if (s[i].getAge() < youngest){
                youngest = s[i].getAge();
            }
        }
            return youngest;
        }
        
        //Finding the oldest age.
        private static int oldestAge(Student s[])
        {
            int oldest = 0;
            for(int i = 0; i < s.length; i++){
            
            if (s[i].getAge() > oldest){
                oldest = s[i].getAge();
            }
        }
            return oldest;
        }
    }// end of class ArrayStudent

  2. #2
    Join Date
    Dec 2005
    Posts
    12
    Hey...have been able to crack it :-)
    Code:
    int youngest = Integer.MAX_VALUE;

  3. #3
    Join Date
    Sep 2006
    Posts
    68
    Hi!

    Please take a look carefully at youngestAge method. You set initial value of youngest eaquals to zero and after this you are making an iteration over the students. But NON of they has age less then 0.

    Solution:
    replace int oldest = 0;
    with int oldest = 1000;

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    A rule of thumb which Uladzimir was applying: when you initialize a comparison (ordering) value which will be evaluated and reset by your program (oldest, youngest, widest, first, last, etc.), initialize with the last valid value in the "opposite" extreme. If you are initializing "youngest", set it to the highest possible valid age (150?), when "oldest", use lowest possible age (0?) - then, the first time you do an evaluation with one of your subjects, you for sure will set the value to a valid and "real" value.

  5. #5
    Join Date
    Dec 2005
    Posts
    12
    Well another solution could be....
    Code:
    int youngest = s[i].getAge();
    But thanks anyway...

Similar Threads

  1. I can't link FreeImage to C#
    By mesh2005 in forum .NET
    Replies: 1
    Last Post: 03-06-2006, 08:16 AM
  2. 'On Error Goto' versus 'Try, Catch, Fail'
    By clarence_rollins in forum .NET
    Replies: 21
    Last Post: 09-11-2002, 11:32 AM
  3. Returning errors from SQL Server Stored Procs
    By Khalizan in forum VB Classic
    Replies: 1
    Last Post: 11-28-2001, 01:32 AM
  4. WinForm Compile Error
    By Robert in forum .NET
    Replies: 1
    Last Post: 10-24-2000, 11:38 AM
  5. Replies: 0
    Last Post: 04-07-2000, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center