Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add two numbers from different classes 1

Status
Not open for further replies.

R17

Programmer
Jan 20, 2003
267
PH
how can i "integrate" these two classes to add no1 and no2?


i have mainprog as,

Code:
import java.io.* ;
public class testprog {
        static int no1, no2 ;

        public static void main (String args[]) throws IOException
        {
                BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
                System.out.print ("First no: ") ;
                no1 = Integer.parseInt(br.readLine()) ;
                System.out.print ("Second no: ") ;
                no2 = Integer.parseInt(br.readLine()) ;
        }
}

and numadd as,

Code:
class numadd extends testprog {
        public static void main (String args[])
        {
                double sum = 0.0 ;
                sum = no1 + no2 ;
                System.out.println ("Sum is "+sum);
        }
}
 
Add 1 line to the main of your first class :

public static void main (String args[]) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("First no: ") ;
no1 = Integer.parseInt(br.readLine()) ;
System.out.print ("Second no: ") ;
no2 = Integer.parseInt(br.readLine()) ;
numadd.main(null);
}

=============
But why do you have a main in numadd ?
You can also do the following :

import java.io.* ;

public class testprog {
static int no1, no2 ;

public static void main (String args[]) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.print ("First no: ") ;
no1 = Integer.parseInt(br.readLine()) ;
System.out.print ("Second no: ") ;
no2 = Integer.parseInt(br.readLine()) ;
System.out.println ("Sum testprog is "+ numadd.add(no1, no2));
}
}

class numadd extends testprog {
public static double add(int a, int b) {
double sum = 0.0 ;
sum = a + b ;
System.out.println ("Sum numadd is "+sum);
return sum;
}

}


 


great help thanks hologram!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top