0

How can we use input from one class to another ?

Need to use input from one class and use it in another class functions

19th Oct 2020, 5:08 PM
Charlie Z
Charlie Z - avatar
2 Answers
0
I need to convert this code into java #include <iostream> using namespace std; class gajjar { double a,b; public: void read() { cout<<"\nEnter two double type numbers:"; cin>>a>>b; } void div() { try{ if(cin.fail()) throw "Bad input!"; if( b == 0 ) throw 0; cout<<"\nAns is "<<a/b; } catch(const int n) { cout << "\nDivision by " << n << " not allowed\n"; } catch(const char* Str) { cout<< Str; } } }; int main() { gajjar k; k.read(); k.div(); return 0; }
19th Oct 2020, 6:57 PM
Charlie Z
Charlie Z - avatar
0
//Tried but not working import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter two double"); double x=sc.nextDouble(); double y=sc.nextDouble(); System.out.println("Double: "+x); System.out.println("Double: "+y); } } public class Example { public static void main(String[] args) { Solution a=new Solution(); double p=a.x; double q=a.y; System.out.println(divisionFunction(p, q)); } public static int divisionFunction(double dividend, double divisor) { int quotient; if (divisor == 0.0) { throw new IllegalArgumentException("Argument 'divisor' is 0"); } else { return quotient = (int) (dividend / divisor); } } }
19th Oct 2020, 7:18 PM
Charlie Z
Charlie Z - avatar