Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Overloading Constructors

807603Jan 31 2008 — edited Jan 31 2008
For some reason the program is not stepping the first Constructor. I thought that if two constructors contained different data types you can overload them. As you can see I have one constructor that is ints and another that is doubles. The problem is that it never hits the int Constructor. Can anyone see why? Here is the code.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.*;
/**
 *
 * @author pberardi
 */
public class Complex {
    double real;
    double imag;
    int real1 =0;
    double imag1 =0;
    int real2 =0;
    double imag2 =0;
    
  +  public Complex(int r1, int r2){
        real1 = r1;
        real2 = r2;+
       
    }
  *  public Complex(double i1, double i2){
        imag1 = i1;
        imag2 = i2;*
    }
   /* public double getReal(){
        return real;
    }
    public double getImag(){
        return imag;
    }
    public void setReal(){
        
    }
    public void setImag(){
        
    }      */
    
    public void add(){
        double totalR=0;
        double totalI=0;
        System.out.println("adding your numbers...");
        totalR = real1 + real2;
        totalI = imag1 + imag2;
        System.out.print(totalR);
        System.out.println(totalI);
    }
    
    public double subtract(){
        double TotalR=0;
        System.out.println("subtracting your numbers...");
      //  return getReal();
        return TotalR;
    }
    
    public double multiply(){
        int totalR =0;
        System.out.println("multiplying your numbers...");
        return totalR;
       // return getReal();
        
    }
    public double divide(){
        int totalR=0;
        
        System.out.println("dividing your numbers");
       // return getReal();
        return totalR;
       
    }
    public  static void input(){
        
        System.out.println("Hello and Welcome to complex numbers");
        Scanner sc = new Scanner(System.in);
        int firstInputReal =0;
        int firstInputImag =0;
        System.out.println("First equation");
        System.out.println("Your equation should be in the for a +bi");
        System.out.println("Enter the 'a' part of the equation   ");
        firstInputReal = sc.nextInt();
        System.out.println();
        System.out.println("Now enter the 'b' part of the equation   ");
        firstInputImag = sc.nextInt();

        System.out.println();
        
        double secondInputReal =0;
        double secondInputImag =0;
        System.out.println("Second equation");
        System.out.println("Your equation should be in the for a +bi");
        System.out.println("Enter the 'a' part of the equation   ");
        secondInputReal = sc.nextDouble();
        System.out.println();
        System.out.println("Now enter the 'b' part of the equation   ");
        secondInputImag = sc.nextDouble();
        System.out.println();
        //System.out.println(inputReal);
        //System.out.println(inputImag);
        System.out.println("");
        System.out.println("Now tell me if you would like to add, subtract, multiply or divide");
        System.out.println();
        System.out.println("1 for add");
        System.out.println("2 for subtract");
        System.out.println("3 for multiply");
        System.out.println("4 for divide");
        int menuChoice = 0;
        menuChoice = sc.nextInt();
         +Complex real = new Complex(firstInputReal, secondInputReal);+
         *Complex imag = new Complex(firstInputImag, secondInputImag);*
        switch(menuChoice){
            case 1:
               
                real.add();
                imag.add();
                break;
                
            case 2:
                
                real.subtract();
                imag.subtract();
                
            case 3:
                
                real.multiply();
                imag.multiply();
                
            case 4:
                
                real.divide();
                imag.divide();
                                        
    }
      
       
    }
   
  
    
    public static void main( String args[] ) {
        input();  
      
           
                
        }
        
    
        
        
        
        
        
    
    

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 28 2008
Added on Jan 31 2008
6 comments
283 views