?
Solved

Java Constructor overloading

Posted on 2009-02-23
10
Medium Priority
?
204 Views
Last Modified: 2012-05-06
Pls can you advice how to add a constructor overloading using super().? Also, I'd like to add a new method to calculate the SUM of the array.

TIA,
V.
import java.io.*;
import java.util.Scanner;
import java.util.Random;
 
public class Class4( int[]  pack1 ) {
 this.pack1 = pack1 ;
  }
protected int[]  pack1 ; 
 
 
  int    i;
  int[]  pack1      = new int[20]; //pack1[0],  ..... pack1[19]
  Random randomizer = new Random();
 
  public Class4() {
 
  }
  public static Class4 getInstance(){
    return new Class4();
  }
 
  public static void main( String[] args ) {
    Class4 instance1 = Class4.getInstance();
    instance1.makeArray();
    instance1.dummy();
 
    Class4 instance2 = Class4.getInstance();
    instance2.makeArray();
    instance2.dummy();
    // 2) Print them out
    for ( int i = 0; i < 20; i++ ) {
      System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 3) Print them out in reverse order
    System.out.println( "In reverse order that is:" );
    for ( int i = 19; i >= 0; i-- ) {
        System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 4) Print out the even numbers only
    for ( int i = 0; i < 20; i++ ) {
      if ( instance1.pack1[i] % 2 == 0 )
          System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
  } //main
 
  private void dummy() {
    System.out.println("I a dummy method");    
  }
  public void makeArray() { // new function
    // 1) Fill in with random numbers
    for ( int i = 0; i < 20; i++ ) {
      pack1[i] = randomizer.nextInt( 1000 );
    }
  }
  
  public void print() {
          for ( int i = 0; i < 20; i++ ) {
              System.out.println(pack1[i]);
            }
        
  }
} //Class4

Open in new window

0
Comment
Question by:Nakuru1234
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23711061
Constructors are not actually overloaded in Java - they're just separate ctors. That aside, how/why would you want to 'overload' it?
0
 

Author Comment

by:Nakuru1234
ID: 23711122
I'd like to see how Java performs when you overload using super().

TIA,
V.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23711644
In this case, the only use of super would be:
public class Class4( int[]  pack1 ) {
    super(); //Call the superclass (Object) empty ctor
    this.pack1 = pack1 ;
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Nakuru1234
ID: 23711818
Great! Thank you.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23712191
Any other questions on that?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23712194
Anythng else on that?
0
 

Author Comment

by:Nakuru1234
ID: 23712242
No...thanks! Will close it up...thank you so much! This is how I assume you meant...right?

import java.io.*;
import java.util.Scanner;
import java.util.Random;
 
public class Class4( int[]  pack1 ) {
    super(); //Call the superclass (Object) empty ctor
 this.pack1 = pack1 ;
  }
protected int[]  pack1 ;

 
  int    i;
  int[]  pack1      = new int[20]; //pack1[0],  ..... pack1[19]
  Random randomizer = new Random();
 
  public Class4() {
 
  }
  public static Class4 getInstance(){
    return new Class4();
  }
 
  public static void main( String[] args ) {
    Class4 instance1 = Class4.getInstance();
    instance1.makeArray();
    instance1.dummy();
 
    Class4 instance2 = Class4.getInstance();
    instance2.makeArray();
    instance2.dummy();
    // 2) Print them out
    for ( int i = 0; i < 20; i++ ) {
      System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 3) Print them out in reverse order
    System.out.println( "In reverse order that is:" );
    for ( int i = 19; i >= 0; i-- ) {
        System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 4) Print out the even numbers only
    for ( int i = 0; i < 20; i++ ) {
      if ( instance1.pack1[i] % 2 == 0 )
          System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
  } //main
 
  private void dummy() {
    System.out.println("I a dummy method");    
  }
  public void makeArray() { // new function
    // 1) Fill in with random numbers
    for ( int i = 0; i < 20; i++ ) {
      pack1[i] = randomizer.nextInt( 1000 );
    }
  }
 
  public void print() {
          for ( int i = 0; i < 20; i++ ) {
              System.out.println(pack1[i]);
            }
       
  }
} //Class4
0
 
LVL 92

Expert Comment

by:objects
ID: 23716493
you wouldn't do it like that :)  You ctor and member vars ned to be included in the class. Plus calling super() is actually impleied so it is not necessary to make that call explicitly.

import java.io.*;
import java.util.Scanner;
import java.util.Random;
 
  public class Class4 {
 
public class Class4( int[]  pack1 ) {
    super(); //Call the superclass (Object) empty ctor *** this line is implied and can re removed
 this.pack1 = pack1 ;
  }
protected int[]  pack1 ;

 
  int    i;
  int[]  pack1      = new int[20]; //pack1[0],  ..... pack1[19]
  Random randomizer = new Random();
 

  public static Class4 getInstance(){
    return new Class4();
  }
 
  public static void main( String[] args ) {
    Class4 instance1 = Class4.getInstance();
    instance1.makeArray();
    instance1.dummy();
 
    Class4 instance2 = Class4.getInstance();
    instance2.makeArray();
    instance2.dummy();
    // 2) Print them out
    for ( int i = 0; i < 20; i++ ) {
      System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 3) Print them out in reverse order
    System.out.println( "In reverse order that is:" );
    for ( int i = 19; i >= 0; i-- ) {
        System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
 
    // 4) Print out the even numbers only
    for ( int i = 0; i < 20; i++ ) {
      if ( instance1.pack1[i] % 2 == 0 )
          System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
  } //main
 
  private void dummy() {
    System.out.println("I a dummy method");    
  }
  public void makeArray() { // new function
    // 1) Fill in with random numbers
    for ( int i = 0; i < 20; i++ ) {
      pack1[i] = randomizer.nextInt( 1000 );
    }
  }
 
  public void print() {
          for ( int i = 0; i < 20; i++ ) {
              System.out.println(pack1[i]);
            }
       
  }
} //Class4

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 23716516
you have a few other problems, including a missing constructor.
Not sure exactly what you need but the following should at least compile
Let me know if you have any questions

import java.util.Random;

public class Class4 {

      public Class4() {
            super();
            makeArray();
      }
      
      public Class4(int[] pack1) {
            super(); // Call the superclass (Object) empty ctor *** this line is
                              // implied and can re removed
            this.pack1 = pack1;
      }

      int i;
      int[] pack1 = new int[20]; // pack1[0], ..... pack1[19]
      Random randomizer = new Random();

      public static Class4 getInstance() {
            return new Class4();
      }

      public static void main(String[] args) {
            Class4 instance1 = Class4.getInstance();
            instance1.makeArray();
            instance1.dummy();

            Class4 instance2 = Class4.getInstance();
            instance2.makeArray();
            instance2.dummy();
            // 2) Print them out
            for (int i = 0; i < 20; i++) {
                  System.out.println(instance1.pack1[i] == instance2.pack1[i]);
            }

            // 3) Print them out in reverse order
            System.out.println("In reverse order that is:");
            for (int i = 19; i >= 0; i--) {
                  System.out.println(instance1.pack1[i] == instance2.pack1[i]);
            }

            // 4) Print out the even numbers only
            for (int i = 0; i < 20; i++) {
                  if (instance1.pack1[i] % 2 == 0)
                        System.out.println(instance1.pack1[i] == instance2.pack1[i]);
            }
      } // main

      private void dummy() {
            System.out.println("I a dummy method");
      }

      public void makeArray() { // new function
            // 1) Fill in with random numbers
            for (int i = 0; i < 20; i++) {
                  pack1[i] = randomizer.nextInt(1000);
            }
      }

      public void print() {
            for (int i = 0; i < 20; i++) {
                  System.out.println(pack1[i]);
            }

      }
} // Class4

0
 

Author Closing Comment

by:Nakuru1234
ID: 31550072
Perfect!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question