Monday, October 18, 2021

HANDS ON JAVA(OOP - based uses) (Part-III)

     Practice JAVA like Never Before!

                                                                                                     courtesy: Google Images

Day 3 🔥

Objective(s)

Learning to use java.io.Console class, pass by value vs pass by reference, method returning object.

1. Add two floating-point numbers by taking input using java.io.Console class.

  • Program

import java.io.Console;
class A3a{
    public static void main(String[] args){
Console c = System.console();   
System.out.println("Enter two floating-point numbers: ");   
float a = Float.parseFloat(c.readLine());
float b = Float.parseFloat(c.readLine());
System.out.println("Required sum = "+(a+b));
    }
}
  • Output:

---------------------------------------------------

2. Develop a program to swap two numbers using the pass by value method.

  • Program:

import java.util.*;

class Swapp{
  static void passByVal(int x, int y){
      x = x^y;
  y = x^y;
  x = x^y;
  System.out.println("   x = " +x+",   "+"y = "+y+'\n');
    }
public static void main(String[] args){
System.out.print("Enter two numbers: ");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
System.out.print("\nBefore swapping : ");
System.out.println("  x = "+num1+",   "+"y = "+num2);
System.out.print("\nAfter swapping : ");
passByVal(num1, num2);
    }
}
  • Output:

---------------------------------------------------


3. Develop a program to swap two numbers using the pass by reference method.

  • Program:

import java.util.Scanner;
class Swappy{
  int n1, n2;
  Swappy(int x, int y){
    n1 = x;
n2 = y;
  }
  void swap(Swappy obj){
    int tmp;
tmp = obj.n2;
obj.n2 = obj.n1;
obj.n1 = tmp;
  }
}
class Swapp{
  public static void main(String[] args){
System.out.print("Enter two numbers: ");
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int num2 = sc.nextInt();
Swappy objj = new Swappy(num1, num2);
System.out.print("\nBefore swapping : ");
System.out.println("  x = "+objj.n1+",   "+"y = "+objj.n2);
System.out.print("\nAfter swapping : ");
objj.swap(objj);
System.out.println("   x = "+objj.n1+",   "+"y = "+objj.n2);
}
}
  • Output:

--------------------------------------------------

4. Create a class; make its instance variable and instance method. Use them from main() which is declared in a different class.

  • Program:

import java.util.Scanner;
class Argand{
double real;
double imaginary;
Argand(){
real = imaginary = 0.0;
}
Argand(double x, double y){
real = x;
imaginary = y;
}
Argand multiply(Argand C){
Argand obj = new Argand();
obj.real = real*C.real - imaginary*C.imaginary;
obj.imaginary = real*C.imaginary + imaginary*C.real;
return obj; //”object returned by method”
}
void disp(){
System.out.println("Result = ("+real+" + "+imaginary+"*i)");
}
}
class A3d{
static public void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the abscissa and ordinate for the 1st point: ");
int x1 = sc.nextInt();
int y1 = sc.nextInt();
System.out.println("Enter the abscissa and ordinate for the 2nd point: ");
int x2 = sc.nextInt();
int y2 = sc.nextInt();
Argand obj1 = new Argand(x1, y1);
Argand obj2 = new Argand(x2, y2);
Argand obj = new Argand();
obj = obj1.multiply(obj2);
obj.disp();
}
}
  • Output:


--------------------------------------------------

5. Write a Java program to make a Student class with proper attributes like roll, name, stream, college and grade. From main() create such two students and show their information.

  • Program:

import java.util.Scanner;
class Student{
  private int r;
  private String n, s, c, g;
  Student(int r, String n, String c, String g, String s){
this.r = r; this.n = n; this.s = s; this.c = c;
this.g = g;
  }
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
System.out.print("\nEnter student 1 roll: ");
int r1 = sc.nextInt();
System.out.print("\nEnter student 1 name: ");
String n1 = sc.next();
System.out.print("\nEnter student 1 college: ");
String c1 = sc.next();
System.out.print("\nEnter student 1 grade: ");
String g1 = sc.next();
System.out.print("\nEnter student 1 stream: ");
String s1 = sc.next();
System.out.print("\nEnter student 2 roll: ");
int r2 = sc.nextInt();
System.out.print("\nEnter student 2 name: ");
String n2 = sc.next();
System.out.print("\nEnter student 2 college: ");
String c2 = sc.next();
System.out.print("\nEnter student 2 grade: ");
String g2 = sc.next();
System.out.print("\nEnter student 2 stream: ");
String s2 = sc.next();
Student obj1 = new Student(r1, n1, c1, g1, s1);
Student obj2 = new Student(r2, n2, c2, g2, s2);
System.out.println("\n-------------\n\nFirst student: ");
System.out.println("Roll = "+obj1.r+", Name = "+obj1.n+", College = "+obj1.c+", Grade = "+obj1.g+", Stream = "+obj1.s);
System.out.println("\n----------------\n\nSecond student: ");
System.out.println("Roll = "+obj2.r+", Name = "+obj2.n+", College = "+obj2.c+", Grade = "+obj2.g+", Stream = "+obj2.s);
  }
}
  • Output:

--------------------------------------------------

<---------- End of Part - III ---------->😋                   

No comments:

Post a Comment

You may like these:

HANDS ON JAVA(OOP - based uses) (Part-I)

     Practice JAVA like Never Before!                                                                                                     ...