Question

In: Computer Science

Create separate class with these members a, b, c, x, y, z int a b c...

Create separate class with these members

a, b, c, x, y, z

int a b c

float x y z

Demonstrate

1) A two arg float constructor, and a two arg int constructor to instantiate objects, initialize variables read from the keyboard, display the sum.

Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)

Solutions

Expert Solution

The following is the code for the above question. The question says about the concept of Constructor Overloading. It is nothing but having one or more constructors but with different arguments. Here each constructor performs a different task. Here constructor works based on the input arguments we pass to it. The below code helps you to understand more.

import java.util.*;
public class Main
{
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
            System.out.println("Enter the two float numbers");
            float f1,f2,sum_f;
            f1=in.nextFloat();
            f2=in.nextFloat();
                Check c1=new Check(f1,f2);
                sum_f=c1.x+c1.y;
                System.out.printf("The sum of given float values is %.2f\n",sum_f);
                System.out.println("Enter the two int numbers");
            int i1,i2,sum_i;
            i1=in.nextInt();
            i2=in.nextInt();
                Check c2=new Check(i1,i2);
                sum_i=c2.a+c2.b;
                System.out.printf("The sum of given int values is %d",sum_i);
                
        }
}
class Check  //declared a class named Check
{
    int a,b,c;  //delare int varibales
    float x,y,z;  //declare float variables
    Check(int a,int b)  //constructor which accepts two int arguments
    {
        this.a=a;
        this.b=b;
    }
    Check(float x,float y)  //constructor which accepts two float arguments
    {
        this.x=x;
        this.y=y;
    }
}

I am also attaching the output for your reference.

Output:

Enter the two float numbers
3.5
4.6
The sum of given float values is 8.10
Enter the two int numbers
8
14
The sum of given int values is 22

#Please dont forget to upvote if you find the solution helpful. Thank you.


Related Solutions

Create separate class with these members a, b, c, x, y, z int a b c...
Create separate class with these members a, b, c, x, y, z int a b c float x y z Demonstrate 3) A two arg float, int constructor, and a three arg int, float, float constructor to instantiate objects, initialize variables read from the keyboard, display the sum Note:- Please type and execute this above java program and also give the output for both problems. (Type a java program)
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
L= {x^a y^b z^c | c=a+b(mod 2)} .Create a DFA and NFA for the language L....
L= {x^a y^b z^c | c=a+b(mod 2)} .Create a DFA and NFA for the language L. Solution and explanation please..
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
What are (a) the x component, (b) the y component, and (c) the z component of...
What are (a) the x component, (b) the y component, and (c) the z component of r Overscript right-arrow EndScripts equals a Overscript right-arrow EndScripts minus b Overscript right-arrow EndScripts plus c Overscript right-arrow EndScripts if a Overscript right-arrow EndScripts equals 5.4 i Overscript ̂ EndScripts plus 1.9 j Overscript ̂ EndScripts minus 3.6 k Overscript ̂ EndScripts , b Overscript right-arrow EndScripts equals negative 4.1 i Overscript ̂ EndScripts plus 5.4 j Overscript ̂ EndScripts plus 3.7 k Overscript...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
which would evaluate to false? ((x&&!y ||z) || !z&&x) a. x and y are false b....
which would evaluate to false? ((x&&!y ||z) || !z&&x) a. x and y are false b. x and z are false c. z is false d. x y z are true e. x is false
give a) Domain b) VA c) HA* d) OA* e) y-int f) x-int for each 2)...
give a) Domain b) VA c) HA* d) OA* e) y-int f) x-int for each 2) f(x) = 4 /3x - 9 3) g(x) = (x -1)(x + 4) /(x + 1)(x - 5) 4) h(x) = x ^2 + 4x/ x + 6 5) j(x) = x ^2 - 4 /(x + 2)(x - 3)
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
if A,B, and C are true statements and X,Y, and Z are false statements, determine which...
if A,B, and C are true statements and X,Y, and Z are false statements, determine which of the following are true : 1. ~[(B > Z) * ~ (A * Y)] 2. [A * (Z > C)] V ~ [(C > B) V ~(X * ~B)] 3. {[(A * B) > ~ (~C V ~Z)] * ( ~Y V Z)} * [(X > ~B) >A] 4. ~{[A > ~(Z > C)} > [(A * ~B) V (Y > ~Z]} *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT