JAVA coding

Asked 2 years ago, Updated 2 years ago, 26 views

Hello, I need Java coding, but I tried source coding, so please modify it --ㅜ It's a question of completing a program given a class with only attributes The class name with main is A01, and

class Box1 { int a; int b; } is given.

Create two objects using classBox1. Store an arbitrary integer value in the object property variable of each object. Write a program that adds the value of a of two objects and outputs the value of b of two objects. Please give us a quick answer~~~~~~~~~~- class Box1 { int a; int b; } public class A01 { public static void main(String args[]) { Box1 mybox1 = new Box1(); Box1 mybox2 = new Box1(); int vol1, vol2;

    Box1.a = 78;
    Box1.b = 145;

    Box2.a = 48;
    Box2.b = 45;


    vol1 = mybox1.width * Box1.a + Box1.b;
    System.out.println ("the sum of the first boxes is " + vol1 + "";

    vol2 = mybox2.width * Box2.a + Box2.b;;
    System.out.println ("the sum of the second boxes is " + vol2 + "";

} }

java

2022-09-22 18:00

1 Answers

class A01{
    static class Box1{
        public Box1(int a, int b){
            this.a = a;
            this.b = b;
        }
        private int a;
        private int b;
        public int getA(){
            return this.a;
        }
        public int getB(){
            return this.b;
        }
    }
    public static void main(String[] args){
        Box1 box1 = new Box1(10, 20);
        Box1 box2 = new Box1(20, 30);
        System.out.println("box1.a + box2.a = " + sum(box1.getA(), box2.getA()));
        System.out.println("box1.b + box2.b = " + sum(box1.getB(), box2.getB()));
    }
    private static int sum(int a, int b){
        return a+b;
    }
}


2022-09-22 18:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.