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;publicclassStudent{
String name;int roll_no;}publicclassTestStudent{publicstaticvoidmain(String[] args){
Student s =newStudent();
s.name ="John";
s.roll_no =2;
System.out.println("Name is "+ s.name +" add roll number is "+ s.roll_no);}}
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;publicclassComplex{int real;int imag;publicComplex(int r,int i){
real = r;
imag = i;}publicstatic Complex add(Complex a, Complex b){returnnewComplex((a.real + b.real),(a.imag - b.imag));}publicstatic Complex diff(Complex a, Complex b){returnnewComplex((a.real - b.real),(a.imag - b.imag));}publicstatic Complex product(Complex a, Complex b){returnnewComplex(((a.real * b.real)-(a.imag * b.imag)),((a.real * b.imag)+(a.imag * b.real)));}publicvoidprintComplex(){if(real ==0&& imag !=0){
System.out.println(imag +"i");}elseif(imag ==0&& real !=0){
System.out.println(real);}else{
System.out.println(real +"+"+ imag +"i");}}}publicclassTestComplex{publicstaticvoidmain(String[] args){
Complex c =newComplex(4,5);
Complex d =newComplex(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
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;publicclassArea{int length;int breadth;publicArea(int l,int d){
length = l;
breadth = d;}publicintgetArea(){return length * breadth;}}publicclassTestArea{publicstaticvoidmain(String[] args){
Scanner s =newScanner(System.in);int l, b;
System.out.println("Enter length");
l = s.nextInt();
System.out.println("Enter breadth");
b = s.nextInt();
Area a =newArea(l, b);
System.out.println("Area : "+ a.getArea());}}
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;publicclassRectangle{int length;int breadth;publicRectangle(int l,int b){
length = l;
breadth = b;}publicintgetArea(){return length * breadth;}publicintgetPerimeter(){return2*(length * breadth);}}publicclassTestRectangle{publicstaticvoidmain(String[] args){
Rectangle a =newRectangle(4,5);
Rectangle b =newRectangle(5,8);
System.out.println("Area : "+ a.getArea()+" Perimeter is "+ a.getPerimeter());
System.out.println("Area : "+ b.getArea()+" Perimeter is "+ b.getPerimeter());}}
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.
publicclassTriangle{int a, b, c;publicdoublegetArea(){double s =(a + b + c)/2.0;return Math.pow((s *(s - a)*(s - b)*(s - c)),.5);}publicdoublegetPerimeter(){return(a + b + c);}}publicclassTestTriangle{publicstaticvoidmain(String[] args){
Triangle t =newTriangle();
t.a =2;
t.b =5;
t.c =6;
System.out.println(t.getArea());
System.out.println(t.getPerimeter);}}
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;publicclassMatrix{int row;int column;int[][] arr;publicMatrix(int r,int c){
row = r;
column = c;
arr =newint[row][column];}publicintgetRows(){return row;}publicintgetColumns(){return column;}publicintgetElement(int r,int c){return arr[r][c];}publicvoidsetElement(int r,int c,int element){
arr[r][c]= element;}publicstatic Matrix add(Matrix x, Matrix y){if((x.row == y.row)&&(x.column == y.column)){
Matrix m =newMatrix(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");returnnewMatrix(0,0);}}publicstatic Matrix product(Matrix x, Matrix y){
Matrix m =newMatrix(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;}publicvoidprintMatrix(){
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]+"\t");}
System.out.println();}}}publicclassTestMatrix{publicstaticvoidmain(String[] args){
Matrix m =newMatrix(3,3);
Matrix n =newMatrix(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();}}