Google Answers Logo
View Question
 
Q: java ( No Answer,   1 Comment )
Question  
Subject: java
Category: Computers > Programming
Asked by: cb153-ga
List Price: $2.00
Posted: 24 Jun 2005 08:45 PDT
Expires: 24 Jul 2005 08:45 PDT
Question ID: 536639
Could anybody answer this question?

Complete the implementation of the constructor and the methods, run and toString.

constructor
?	accepts a parameter size
?	instantiates anarray of length size
?	declares a random object
?	uses the random object to fill the array

run( )
?	declares a variable to store the maximum value in the array
?	examines the array using Math.max to determine the largest value
?	displays "Max value is" and the value of max

toString( )
?	returns a String "Array contents" followed by a list of numbers in
the array, seperated by spaces

Example output
Array contents - -171976890 -837367250 588580956 1404222675 -254609160
1878654740 -16822294198 1602621976 -556422055 334633135
Max value is 1878654740

Examq4.java

public class Examq4   {
    private int[]a;
    
    public Examq4(int size)   {
        



    }

    public void run( )   {
        int max;




    }

    public String toString( )   {
        String result = "Array contents";



        return result;
    }

    public static void main(String[] args)   {
        Examq4 q4 = new Examq4(10);
        System.out.println(q4);
        q4.run( );
    }
}
Answer  
There is no answer at this time.

Comments  
Subject: Re: java
From: bob215-ga on 24 Jun 2005 15:24 PDT
 
Hi cb153-ga thanks for giving me chance to answer your question here
is the completed implementation

import java.util.*;

public class Examq4{

        private int[]a;

        int n;
        public Examq4(int size){

        int i;
        a = new int[size];
        Random rand = new Random();
        for(i=0;i<size;i++)
        {
        a[i]=rand.nextInt();
        }
        n=a.length;
        }


        public void run()
        {
         int max;
         if(n!=0)
         {
         max=a[0];
         for(int i=1;i<n-1;i++)
         {
         max=Math.max(max,a[i]);
         }
         System.out.println("Max value is "+max);
         }

        }


        public String toString()
        {             
          int i;
          String result="Array contents:";
          if (n!=0)
          {
          for(i=0;i<n;i++)
          {
          result+=" "+a[i];
          }}
          else
          result+=" Empty";
          return result;
        }

        public static void main(String args[]) 
        {
         Examq4 q4=new Examq4(10);
         System.out.println(q4.toString());
         q4.run();
         }
         
        }

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy