0% found this document useful (0 votes)
12 views29 pages

java exp 13 to 19

The document contains Java code for matrix multiplication and explanations of arrays, vectors, and wrapper classes. It discusses array declaration, memory allocation, and the differences between arrays and vectors. Additionally, it covers methods of the Integer and Character wrapper classes, including their conversion and utility methods.

Uploaded by

esla4517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views29 pages

java exp 13 to 19

The document contains Java code for matrix multiplication and explanations of arrays, vectors, and wrapper classes. It discusses array declaration, memory allocation, and the differences between arrays and vectors. Additionally, it covers methods of the Integer and Character wrapper classes, including their conversion and utility methods.

Uploaded by

esla4517
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

import java.util.

*;
public class MultidimensionalArray
{
static int N = 4;
static void mul(int a[][],int b[][], int c[][])
{
int i, j, k;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
c[i][j] = 0;
for (k = 0; k < N; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
}
public static void main (String[] args)
{
int a[][] = { {9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3}};
int b[][] = {{ 9, 7, 2, 3}, {9, 7, 2, 3},{9, 7, 2, 3},{9, 7, 2, 3}};
int c[][] = new int[N][N] ;
int i, j;
mul(a, b, c);
System.out.println("First Matrix is : \n ");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( a[i][j] + " ");
System.out.println();
}
System.out.println("Second Matrix is: \n ");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( b[i][j] + " ");
System.out.println();
}
System.out.println("Multiplication result matrix is \n");
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
System.out.print( c[i][j] + " ");
System.out.println();
}
}
}

Ans 1:
Instantiating an Array in Java
• When an array is declared, only a reference of array is created.
• To allocates memory to array, new keyword is used.
• Syntax:
array-var-name = new type [size];
• Here,
type specifies the type of data being allocated,
size specifies the number of elements in the array,
var-name is the name of array variable that is linked to the array.
• Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one

Ans 2:
• Second dimension in 2D-array is optional in Java.
• We can create a two dimensional array without specifying second dimension e.g.
int[4][] is valid array declaration.
• The scenario where you want every row to a different number of columns, in such
cases this type of array declaration is useful.
• A multi-dimensional array is an array of array.
• For example two dimensional array in Java is simply an array of one dimensional
array like int[][] is an array of array of int[] or "array of array of int".
• This diagram shows how exactly two dimensional arrays are stored in Java :

• Example:
class Demo
{
public static void main(String[] args)
{
int a[][] = new int[2][];
a[0] = new int[2];
a[1] = new int[4];
}
}
Ans 3:

• In Java an array cannot change its size once it is created.

Ans 4:

• Consider following program:

class ArrayExample
{
public static void main(String[] args)
{
int[] age = new int[5];
System.out.println(age[0]);
System.out.println(age[1]);
System.out.println(age[2]);
System.out.println(age[3]);
System.out.println(age[4]);
System.out.println(age[5]);
}
}
• Line #11. Save, Compile & Run the code, then an exception will raised by java
compiler saying ware trying to access array index which is out of bound, So when
we try to access array element out of index java throws exception.
• Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
ArrayDemo.main(ArrayExample.java:11)

Ans 3:

public class Test


{
public static void main(String args[])
{
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers )
System.out.println( x+”,” );

String [] names ={“Ankit", “Rahul", “Sam”};


for( String name : names )
System.out.print( name+”,” );
}
}
import java.util.*;
class VectorAddMethods
{
public static void main(String[] arg)
{
Vector v = new Vector();
v.add(new Integer(1));
v.add(2);
v.add(1,"thakur");
v.add(2,"Polytechnic");
v.addElement(new Character('A'));
System.out.println("\n Elements of Vector v are : " + v);
}
}

Ans1:

• capacity() method gives the capacity of the vector, means it returns the maximum
number of element a vector can hold.
• size() method returns the number of elements currently hold by vector.

Ans 2:

• addElement() method adds specified element at the end of the vector also
increasing its size by 1.
• insertElementAt() method inserts the specified element at specified position and
moves all the elements one step further.

Ans 3:

Sr.No. Array Vector


1 The length of array is fixed. A Vector is a resizable.
2 A Vector is synchronized, Array is not synchronized.
3 Java arrays can hold both primitive Vector can hold only Java objects.
data types and Java objects.
4 To find the size of the Vector, we Array has a length property that stores
can call its size() method, its length.
5 Array supports dimensions (single- A Vector has no concept of
dimensional or multidimensional) dimensions.
6 Array is not a class. A vector is a class.

import java.util.*;
class VectorClassMethods
{
public static void main(String[] arg)
{
Vector v = new Vector(); // capacity-->10
System.out.println("\n Capacity of Vector v is " + v.capacity());
System.out.println("\n Size of Vector v is " + v.size());
v.add(new Integer(1)); //v=[1]
v.add(2); //v[1,2]
v.add(1,"thakur"); //v=[1,"thakur",2]
v.add(2,"Polytechnic"); //v=[1,"thakur","Polytechnic",2]
v.add(new Integer(1)); //v=[1,"thakur","Polytechnic",2,1]
v.add(2); //v=[1,"thakur","Polytechnic",2,1,2]
System.out.println("\n Capacity of Vector v is " + v.capacity());
System.out.println("\n Size of Vector v is " + v.size());
System.out.println("\n Elements of Vector v are : " + v);

v.add(1,"thakur"); //v=[1,"thakur","thakur","Polytechnic",2,1,2]
v.add(2,"Polytechnic"); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2]
v.add(new Integer(1)); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1]
v.add(2); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1,2]
v.add(22); //v=[1,"thakur","Polytechnic","thakur",
"Polytechnic",2,1,2,1,2,22]
//v.addElement(new Character('A'));
System.out.println("\n Vector v is " + v);

System.out.println("\n Capacity of Vector v is :" + v.capacity());


System.out.println("\n Size of Vector v is " + v.size());

System.out.println("\n Is Vector v Empty: " + v.isEmpty()); //false


System.out.println("\n Value at first index :" +v.firstElement()); //1
System.out.println("\n Value at last index :" +v.lastElement()); //22
System.out.println("\n Value at index 2: " +v.get(2));
//"Polytechnic"
System.out.println("\n Index of first ocurance of element 1 is : " +v.indexOf(1));
//0
System.out.println("\n Index of Last occurance of element 1 is :
"+v.lastIndexOf(1));//8
System.out.println("\n Remove first occouranc of element 1 is : "
+v.remove((Integer)1)); // remove 1 from index 0-true
System.out.println("\n Vector v is " + v);
//v=["thakur","Polytechnic","thakur", "Polytechnic",2,1,2,1,2,22]

v.setElementAt(1,0);
System.out.println("\n Vector v is after adding 1 at index 0 :" + v);
//v=[1,"Polytechnic","thakur", "Polytechnic",2,1,2,1,2,22]
System.out.println("\n Is vector v contains elemnent 2? :" +v.contains(2)); //true

Vector v1= (Vector) v.clone();


System.out.println("\n Vector v is " + v);
System.out.println("\n Vector v1 is " + v1);
System.out.println("\n Do vector v and v1 are same : " +v1.equals(v));
v1.clear();
System.out.println("\n Vector v1 is" + v1);
}
}

// Java program to illustrate various Integer methods


public class IntegerWrapperClassMethod
{
public static void main(String args[])
{
int b = 55;
String bb = "45";

Integer x = new Integer(b); // Construct two Integer objects


Integer y = new Integer(bb);
// toString()
System.out.println(b+" toString(b) = "+ Integer.toString(b));

// toHexString(),toOctalString(),toBinaryString() converts into hexadecimal, octal and


binary forms.
System.out.println(b+" toHexString(b) ="+ Integer.toHexString(b));
System.out.println(b+" toOctalString(b) ="+ Integer.toOctalString(b));
System.out.println(b+" toBinaryString(b) ="+ Integer.toBinaryString(b));

// valueOf(): return Integer object an overloaded method takes radix as well.


Integer z = Integer.valueOf(b);
System.out.println("valueOf(b) = " + z);
z = Integer.valueOf(bb);
System.out.println("ValueOf(bb) = " + z);
z = Integer.valueOf(bb, 6);
System.out.println("ValueOf(bb,6) = " + z);

// parseInt(): return primitive int value an overloaded method takes radix as well
int zz = Integer.parseInt(bb);
System.out.println("parseInt(bb) = " + zz);
zz = Integer.parseInt(bb, 6);
System.out.println("parseInt(bb,6) = " + zz);

// decode() : decodes the hex,octal and decimal string to corresponding int values.
String decimal = "45";
String octal = "005";
String hex = "0x0f";
System.out.println("decode(45) = " + Integer.decode(decimal));
System.out.println("decode(005) = " + Integer.decode(octal));
System.out.println("decode(0x0f) = " + Integer.decode(hex));

// rotateLeft and rotateRight can be used to rotate bits by specified distance


int val= 2;
System.out.println("rotateLeft(0000 0000 0000 0010 , 2) ="+ Integer.rotateLeft(val, 2));
System.out.println("rotateRight(0000 0000 0000 1010,1) ="+ Integer.rotateRight(val, 1));
}
}

Ans 1:
1)valueOf() method :
• We can use valueOf() method to create Wrapper object for given primitive or String.
• There are 3 types of valueOf() methods:
1) Wrapper valueOf(String s) :
2) Not supported by Character wrapper class.
Syntax: public static Wrapper valueOf(String s);
2) Wrapper valueOf(String s, int radix) :
• All Wrapper class supports ValueOf() method to create wrapper object for the given
String with specified radix (radix is 2 to 36).
Syntax: public static Wrapper valueOf(String s, int radix);
3) Wrapper valueOf(primitive p) :
• All Wrapper class supports ValueOf() method, used to create a Wrapper object for
the given primitive type.
Syntax: public static Wrapper valueOf(primitive p);

Ans 2:
xxxValue():Converts the value of this Number object to the xxx data type and returns it.
1. byte byteValue()
2. short shortValue()
3. int intValue()
4. long longValue()
5. float floatValue()
6. double doubleValue()

Ans 3:

Primitive Wrapper Class

boolean Boolean

byte Byte
char Character

int Integer
float Float

double Double
long Long

short Short

Ans 1:
class StringToInteger
{
public static void main(String[] args)
{
Integer I = Integer.valueOf("10");
System.out.println(I);
Double D = Double.valueOf("10.0");
System.out.println(D);
Integer I1 = Integer.valueOf("1111", 2); // 8 + 4 + 2 +1=15
System.out.println(I1);
Integer I2 = Integer.valueOf("1111", 4);//
System.out.println(I2);
}
}
Ans 2:
Character Wrapper Class Methods:
• boolean isLetter(char ch) : This method is used to determine whether the specified
char value(ch) is a letter or not.
• boolean isDigit(char ch) : This method is used to determine whether the specified
char value(ch) is a digit or not.
• boolean isWhitespace(char ch) : It determines whether the specified char value(ch)
is white space. A whitespace includes space, tab, or new line.
• boolean isUpperCase(char ch) : It determines whether the specified char value(ch) is
uppercase or not.
• boolean isLowerCase(char ch) : It determines whether the specified char value(ch)
is lowercase or not.
• char toUpperCase(char ch) : It returns the uppercase of the specified char value(ch).
If an ASCII value is passed, then the ASCII value of it’s uppercase will be returned.
• char toLowerCase(char ch) : It returns the lowercase of the specified char value(ch)

public class CharacterWrapperClassMethod


{
public static void main(String[] args)
{
System.out.println("isLetter('A'):"+Character.isLetter('A'));
System.out.println("isLetter('0'):"+Character.isLetter('0'));

System.out.println("\nisDigit('A'):"+Character.isDigit('A'));
System.out.println("isDigit('0'):"+Character.isDigit('0'));
System.out.println("\nisWhitespace('A'):"+Character.isWhitespace('A'));
System.out.println("isWhitespace(' '):"+Character.isWhitespace(' '));
System.out.println("isWhitespace('\\n'):"+Character.isWhitespace('\n'));
System.out.println("isWhitespace('\t'):"+Character.isWhitespace('\t'));
System.out.println("\nisWhitespace(9):"+Character.isWhitespace(9));
System.out.println("isWhitespace(9):"+Character.isWhitespace('9'));

System.out.println("\nisUpperCase('a'):"+Character.isUpperCase('A'));
System.out.println("isUpperCase('a'):"+Character.isUpperCase('a'));

System.out.println("\nisLowerCase('a'):"+Character.isLowerCase('A'));
System.out.println("isLowerCase('a'):"+Character.isLowerCase('a'));

System.out.println("\ntoUpperCase(97):"+Character.toUpperCase('a'));
System.out.println("toUpperCase(97):"+Character.toUpperCase(97));

System.out.println("\noLowerCase('A'):"+Character.toLowerCase('A'));
System.out.println("toLowerCase('65'):"+Character.toLowerCase(65));
}
}
Ans 3:

class IntegerObjectToPrimitive
{
public static void main(String[] args)
{
Integer I = new Integer(130);
System.out.println(I.byteValue());
System.out.println(I.shortValue());
System.out.println(I.intValue());
System.out.println(I.longValue());
System.out.println(I.floatValue());
System.out.println(I.doubleValue());
}
}
class Base
{
public void display()
{
System.out.println("Base Class");
}
}
class Derived extends Base
{
public void display()
{
System.out.println("Derived Class");
}
public static void main( String args[])
{
Base b = new Base();
System.out.println("______________________________________________\n");
System.out.println("Base class object is stored in Base Class reference variable:");
b.display();
System.out.println("________________________________________\n");
System.out.println("Derived class object is stored in Base Class reference variable:");
Base b1 = new Derived();
b1.display();
System.out.println("______________________________________\n");
}
}
Ans 1:

SN Property Overloading Overriding

1 Method signatures Must be different. Must be the same.

Method overloading is
Type of a compile time Method overriding is a run
2
polymorphism polymorphism. time polymorphism.

return type can or While in this, return type must


3 Return type cannot be same. be same or co-variant.

Generally performed in Performed in two classes


4 Class
the same class. through Inheritance.

Method overloading
5 Inheritance may or may not require While method overriding
inheritance. always needs inheritance.

Private/Static/Final
6 Can be overloaded. Cannot be overridden.
method

Subclass method’s access


modifier must be same or
7 Access modifiers Anything or different.
higher than superclass method
access modifier.

Always take care by


Always take care by JVM
8 Method resolution java compiler based on
based on runtime object.
reference type.

Also known as compile-


Also known as runtime
time polymorphism,
9 Polymorphism polymorphism, dynamic
static polymorphism, or
polymorphism, or late binding.
early binding.

10 Performance Better Less


Ans 3:
Rule 1: Only inherited methods can be overridden.
Rule 2: Final and static methods cannot be overridden.
Rule 3: The overriding method must have same argument list.
Rule 4: Use the super keyword to invoke the overridden method from a subclass.
Rule 5: Abstract methods must be overridden by the first concrete (non-abstract)
subclass.

Ans 4:
Super Keyword in Java
1. Use of super with variables:
• When a derived class and base class has data members with same name.
• In example, both base class and subclass have a member maxSpeed. We could
access maxSpeed of base class in subclass using super keyword.
class Vehicle {
int maxSpeed = 120;
}
class Car extends Vehicle {
int maxSpeed = 180;
void display()
{
System.out.println("Maximum Speed: "
+ super.maxSpeed);
}
}
class Test {
public static void main(String[] args)
{
Car small = new Car();
small.display();
}
}

2. Use of super with methods:


• Whenever a parent and child class have same named methods then to resolve
ambiguity we use super keyword.
• In example , if we only call message() then, the current class message() will invoke
but with the use of super keyword, message() method of superclass will invoke.
class Person {
void message() {
System.out.println(“Person class");
}
}
class Student extends Person {
void message() {
System.out.println(“Student class");
}
void display() { super.message(); }
}
class Test {
public static void main(String args[])
{
Student s = new Student();
s.display();
}
}

3. Use of super with constructors:


• super keyword can also be used to access the parent class constructor.
• ‘Super’ can call both parametric as well as non parametric constructors.

• In example, superclass constructor is called using keyword ‘super’ via subclass


constructor.
class Person {
Person() {
System.out.println("Person:Constructor");
}
}
class Student extends Person{
Student() {
super();
System.out.println("Student:Constructor");
}
}
class Test{
public static void main(String[] args)
{
Student s = new Student();
}
}
Output:
Person class Constructor
Student class Constructor
Ans 1:

class SBI
{
void rateOfInterest(float roi)
{
System.out.println("Rate Of Interest (SBI):"+roi);
}
}
class SBM extends SBI
{
void rateOfInterest(float roi)
{
System.out.println("Rate Of Interest (SBM):"+roi);
}
public static void main(String[] args)
{
SBI s=new SBI();
s.rateOfInterest(6.5f);
SBI s1=new SBM();
s1.rateOfInterest(7.5f);
}
}

Ans 3:
class Animal
{
void move()
{
System.out.println("Animal:Method");
}
}

class Dog extends Animal


{
void move()
{
super.move();
System.out.println("Dog:Method");
}
public static void main(String[] args)
{
Dog d=new Dog();
d.move();
}
}
Program for Single Inheritance:
class one
{
public void print_thakur()
{
System.out.println("Thakur");
}
}
class two extends one
{
public void print_poly()
{
System.out.println("Polytechnic");
}
}
public class SingleInheritanceExample
{
public static void main(String[] args)
{
two t = new two();
t.print_thakur();
t.print_poly();
}
}
Program for Multilevel Inheritance:

class one
{
public void print_thakur()
{
System.out.println("Thakur");
}
}
class two extends one
{
public void print_poly()
{
System.out.println("Polytechnic");
}
}
class three extends two
{
public void print_kandivli()
{
System.out.println("Kandivli");
}
}
public class Main
{
public static void main(String[] args)
{
three t = new three();
t.print_thakur();
t.print_poly();
t.print_kandivli();
}
}
Ans 1:

• When one class extends more than one classes then this is called multiple inheritance.
• For example: Class C extends class A and B then this type of inheritance is known as
multiple inheritance. Java doesn’t allow multiple inheritance.

Why Java doesn’t support multiple inheritance?

• Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.

• One of the example of such problem is the diamond problem that occurs in multiple
inheritance.

What is diamond problem?

• Class D extends both classes B & C.


• Now lets assume we have a method show() in class A and class B & C overrides that
method in their own way.
• Here the problem comes – Because D is extending both B & C so if D wants to use the
show() method which method would be called (the overridden method of B or the
overridden method of C). Ambiguity.
• When the programmer tries to call show() method in Class D then the compiler will get
confused about which method would be called? That’s the main reason why Java
doesn’t support multiple inheritance

Ans 2:
Conditions when can we use super keyword:
• When a derived class and base class has data members with same name.
• Whenever a parent and child class have same named methods then to resolve
ambiguity we use super keyword.
• super keyword can also be used to access the parent class constructor.
Ans 3:
Final variables
• When a variable is declared with final keyword, its value can’t be modified,
essentially, a constant.
• This also means that you must initialize a final variable.
• Example:
final int THRESHOLD = 5;

Final methods: Method declared with final keyword is a final method.


• A final method cannot be overridden.
class A
{
final void m1()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void m1() // COMPILE-ERROR! Can't override.
{
System.out.println("Illegal!");
}
}

Ans 4:
1. It can have abstract methods(methods without body) as well as concrete methods
(regular methods with body).
2. A normal class(non-abstract class) cannot have abstract methods.
3. We can not create object of it abstract class as these classes are incomplete, they
have abstract methods that have no body so java wont allows you to create object of
abstract class.
4. It can have final methods.
5. It can have constructor and static methods also.

Ans 1:
import java.util.Scanner;
class student
{
int rollno;
String name;
}

class exam extends student


{
int m1,m2,m3;
}

class result extends exam


{
float total, per;
char grade;
void get()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter roll number and name of student:\t");
rollno=in.nextInt();
name=in.next();
System.out.println("Enter marks of 3 subjects:\t");
m1=in.nextInt();
m2=in.nextInt();
m3=in.nextInt();
}
void calculateGrade()
{
total=m1+m2+m3;
per=total/3.0f;
if(per >= 80)
grade='A';
else if(per >= 60 && per<80)
grade='B';
else if(per >= 40 && per<60)
grade='C';
else
grade='F';
}
void display()
{
System.out.println("Name of student:"+name);
System.out.println("Roll No. of student:"+rollno);
System.out.println("Total marks of student:"+total);
System.out.println("Percentage of student:"+per);
System.out.println("Grade of student:"+grade);
}
public static void main(String[] args)
{
result r=new result();
r.get();
r.calculateGrade();
r.display();
}
}

Ans 2:
import java.util.Scanner;
class RoomDimensions
{
float length;
float width;
float height;
}
class AreaVolume extends RoomDimensions
{
void getDimensions()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter Length , Width and Height of Room (in feets):\t");
length=in.nextFloat();
width=in.nextFloat();
height=in.nextFloat();
}
void displayVolumeArea()
{
System.out.println("Volume of Room="+length*width*height);
System.out.println("Area of Room="+ (2*length*width + 2*width*height +
2*height*length));
}
public static void main(String[] args)
{
AreaVolume av=new AreaVolume();
av.getDimensions();
av.displayVolumeArea();
}
}
Multiple inheritance Using interface:
• Multiple inheritance is not supported in the case of class because of ambiguity.
• However, it is supported in case of an interface because there is no ambiguity.
• It is because its implementation is provided by the implementating class.
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.

Example to implement multiple inheritance using interfaces:


interface Printable
{
void print();
}
interface Showable
{
void show();
}
class InterfaceExample implements Printable, Showable
{
public void print()
{
System.out.println("Hello");
}
public void show()
{
System.out.println("Welcome");
}
public static void main(String args[])
{
InterfaceExample obj = new InterfaceExample();
obj.print();
obj.show();
}
}

Ans 2:

Similarities between class and interface:


• Both can contain any number of variables and methods (class methods have
implementation code whereas the interface methods can only have declarations)
• Both can be written in a file with .java extension, with the name of the
interface/class matching the name of the file.
• The byte code of both appears in a .class file.
• Both can be inherited using Inheritance (extends keyword for classes and
implements keyword for interfaces)

Ans 3:

• It is used to achieve total abstraction.


• Since java does not support multiple inheritance in case of class, but by using
interface it can achieve multiple inheritance by implementing more than one
interfaces in your class.
Ans 1:

Sr. Key Class Interface


No.
Basic A class is instantiated to An interface can never be
1
create objects. instantiated.
Supported A class can have both an Interface can have only abstract
2 Methods abstract as well as concrete methods.
methods.
Multiple Multiple Inheritance is not Interface supports Multiple
3
Inheritance supported. Inheritance.
Supported final, non-final, static and non- Only static and final variables are
4 Variables static variables supported. permitted.

Implementation A class can implement an Interface can not implement an


5 interface. interface, it can extend an interface.

Keyword Declared using class keyword. Declared using interface keyword.


6
Inheritance A class can be inherited using Interface can only be implemented
7 extends keyword. using implements keyword.
Access Class members can be private, Interface only has public members.
8 public, protected etc

Constructor A class can have constructor Interface can not have a constructor.
9 methods.
Ans 2:
interface Area
{
public int areaOfRectangle(int l, int b);
public float areaOfCircle(float r);
}
public class AreaOfShape implements Area
{
public int areaOfRectangle(int l, int b)
{
return l*b;
}
public float areaOfCircle(float r)
{
return 3.14f*r*r ;
}
public static void main(String args[])
{
AreaOfShape obj = new AreaOfShape();
System.out.println("Area Of Rectangle:"+obj.areaOfRectangle(2,4));
AreaOfShape obj1 = new AreaOfShape();
System.out.println("Area Of Circle:"+obj.areaOfCircle(2.5f));
}
}
nextFloat() reads a float value form the user

nextBoolean() reads a boolean value from the user

nextLine() reads a line of text from the user

next() reads a word from the user

nextByte() reads a byte value from the user

nextDouble() reads a double value from the user

nextShort() reads a short value from the user

nextLong() reads a long value from the user

nextInt()

You might also like