Practice questions on Classes and objects Level Basic

Level 1

  1. Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.
package com.codesdope;

public class Student {
    String name;
    int roll_no;
}

public class TestStudent {
    
    public static void main(String[] args) {
        Student s = new Student();
        s.name = "John";
        s.roll_no = 2;
        
        System.out.println("Name is " + s.name + " add roll number is " + s.roll_no);
    }
}
  1. Print the sum, difference and product of two complex numbers by creating a class named 'Complex' with separate methods for each operation whose real and imaginary parts are entered by user.
package com.codesdope;
   
public class Complex {
    int real;
    int imag;
    public Complex(int r, int i) {

        real = r;
        imag = i;
    }
       
    public static Complex add(Complex a, Complex b) {

        return new Complex((a.real + b.real), (a.imag - b.imag));
    }
       
    public static Complex diff(Complex a, Complex b) {

        return new Complex((a.real - b.real), (a.imag - b.imag));
    }
       
    public static Complex product(Complex a, Complex b) {

        return new Complex(((a.real * b.real) - (a.imag * b.imag)), ((a.real * b.imag) + (a.imag * b.real)));
    }
      
    public void printComplex() {
        if (real == 0 && imag != 0) {
            System.out.println(imag + "i");
        } else if (imag == 0 && real != 0) {
            System.out.println(real);
        } else {
            System.out.println(real + "+" + imag + "i");
        }
    }                          
}
   
public class TestComplex {
       
    public static void main(String[] args) {
           
        Complex c = new Complex(4, 5);
        Complex d = new Complex(9, 4);
           
        Complex e = Complex.add(c, d);
        Complex f = Complex.diff(c, d);
        Complex g = Complex.product(c, d);
        e.printComplex();
        f.printComplex();
        g.printComplex();            
    }
}

Output

13 + 9i
-5 + 1i
16 + 61i
  1. Write a program to print the area of a rectangle by creating a class named 'Area' taking the values of its length and breadth as parameters of its constructor and having a method named 'getArea' which returns the area of the rectangle. Length and breadth of rectangle are entered through keyboard.
package com.codesdope;
      
import java.util.Scanner;
      
public class Area {

    int length;
    int breadth;
    public Area(int l, int d) {
        length = l;
        breadth = d;
    }
    public int getArea() {
        return length * breadth;
    }
}
      
public class TestArea {
          
    public static void main(String[] args) {
              
        Scanner s = new Scanner(System.in);
        int l, b;
              
        System.out.println("Enter length");
        l = s.nextInt();
        System.out.println("Enter breadth");
        b = s.nextInt();
              
        Area a = new Area(l, b);
        System.out.println("Area : " + a.getArea());
    }
}
  1. Write a program to print the area of two rectangles having sides (4,5) and (5,8) respectively by creating a class named 'Rectangle' with a method named 'Area' which returns the area and length and breadth passed as parameters to its constructor.
package com.codesdope;
         
public class Rectangle {
    int length;
    int breadth;
    public Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }
    public int getArea() {

        return length * breadth;
    }

    public int getPerimeter() {
            return 2 * (length * breadth);
    }
}
         
public class TestRectangle {
             
    public static void main(String[] args) {
        Rectangle a = new Rectangle(4, 5);
        Rectangle b = new Rectangle(5, 8);
        System.out.println("Area : " + a.getArea() + " Perimeter is " + a.getPerimeter());
        System.out.println("Area : " + b.getArea() + " Perimeter is " + b.getPerimeter());
    }
}
  1. Write a program to print the area and perimeter of a triangle having sides of 3, 4 and 5 units by creating a class named 'Triangle' without any parameter in its constructor.
public class Triangle {
    int a, b, c;
    public double getArea() {
        double s = (a + b + c) / 2.0;
        return Math.pow((s * (s - a) * (s - b) * (s - c)), .5);
    }
    public double getPerimeter() {
        return (a + b + c);
    }
}         

public class TestTriangle {
             
    public static void main(String[] args) {
        Triangle t = new Triangle();
        t.a = 2;
        t.b = 5;
        t.c = 6;
        System.out.println(t.getArea());
        System.out.println(t.getPerimeter);
    }
}
  1. The Matrix class has methods for each of the following: 1 - get the number of rows 2 - get the number of columns 3 - set the elements of the matrix at given position (i,j) 4 - adding two matrices. If the matrices are not addable, "Matrices cannot be added" will be displayed. 5 - multiplying the two matrices
package com.callicoder;

public class Matrix {

    int row;
    int column;
    int[][] arr;

    public Matrix(int r, int c) {
		row = r;
		column = c;
        arr = new int[row][column];
    }

    public int getRows() {
        return row;
    }

    public int getColumns() {
        return column;
    }

    public int getElement(int r, int c) {
        return arr[r][c];
    }

    public void setElement(int r, int c, int element) {
        arr[r][c] = element;
    }

    public static Matrix add(Matrix x, Matrix y) {

        if ((x.row == y.row) && (x.column == y.column)) {

            Matrix m = new Matrix(x.row, x.column);
            for (int i = 0; i < m.row; i++) {
                for (int j = 0; j < m.column; j++) {
                    m.setElement(i, j, (x.getElement(i, j) + y.getElement(i, j)));
                }
            }
            return m;
        } else {
            System.out.println("Matrices can not be added");
            return new Matrix(0, 0);
        }
    }

    public static Matrix product(Matrix x, Matrix y) {

        Matrix m = new Matrix(x.row, y.column);

        for (int i = 0; i < x.row; i++) {

            for (int j = 0; j < y.column; j++) {
                int sum = 0;
                for (int k = 0; k < x.column; k++) {
                    sum = sum + (x.getElement(i, k) * y.getElement(k, j));
                }
                m.setElement(i, j, sum);
            }
        }
        return m;
    }

    public void printMatrix() {

        System.out.println("Matrix is : ");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                System.out.print(arr[i][j] + "	");
            }
            System.out.println();
        }
    }
}

public class TestMatrix {

    public static void main(String[] args) {

        Matrix m = new Matrix(3, 3);
        Matrix n = new Matrix(3, 3);
        int k = 1;

        for (int i = 0; i < 3; i++) {
            for (int j  =0; j < 3; j++) {
                m.setElement(i, j, k);
                k++;
                n.setElement(i, j, k);
                k++;
            }
        }

        m.printMatrix();
        n.printMatrix();

        Matrix o = Matrix.add(m, n);
        o.printMatrix();

        Matrix p = Matrix.product(m, n);
        p.printMatrix();
    }
}

Output

Matrix is : 
1	3	5	
7	9	11	
13	15	17	

Matrix is : 
2	4	6	
8	10	12	
14	16	18	

Matrix is : 
3	7	11	
15	19	23	
27	31	35	

Matrix is : 
96	114	132	
240	294	348	
384	474	564	
原文地址:https://www.cnblogs.com/PrimerPlus/p/13138740.html