Showing posts with label Predefined Methods. Show all posts
Showing posts with label Predefined Methods. Show all posts

Circle (User Define Classes) - Java Sample Program

public class Circle
{
private double radius;

//Default constructor
//Sets the radius to 0
Circle()
{
radius = 0;
}

//Constructor with a parameter
//Sets the radius to the value specified by the parameter r.
Circle(double r)
{
radius = r;
}

//Method to set the radius of the circle.
//Sets the radius to the value specified by the parameter r.
public void setRadius(double r)
{
radius = r;
}

//Method to return the radius of the circle.
//Returns the radius of the circle.
public double getRadius()
{
return radius;
}

//Method to compute and return the area of the circle.
//Computes and returns the area of the circle.
public double area()
{
return Math.PI * Math.PI * radius;
}

//Method to compute and return the perimeter of the circle.
//Computes and returns the area of the circle.
public double perimeter()
{
return 2 * Math.PI * radius;
}

//Method to return the radius, area, perimeter of the circle
//as a string.
public String toString()
{
return String.format("Radius = %.2f, Perimeter = %.2f"+ ", Area = %.2f%n", radius, perimeter(), area());
}
}


We leave the UML class diagram of the class Circle as an exercise for you.
The following program shows how to use the class Circle in a program.
// Program to test various operations of the class Circle.

import java.util.*; //Line 1
public class TestProgCircle //Line 2
{ //Line 3

static Scanner console = new Scanner(System.in); //Line 4

public static void main(String[] args) //Line 5
{ //Line 6

Circle firstCircle = new Circle(); //Line 7
Circle secondCircle = new Circle(12); //Line 8

double radius; //Line 9

System.out.println("Line 10: firstCircle: " + firstCircle); //Line 10

System.out.println("Line 11: secondCircle: " + secondCircle); //Line 11

System.out.print("Line 12: Enter the radius: "); //Line 12

radius = console.nextDouble(); //Line 13

System.out.println(); //Line 14

firstCircle.setRadius(radius); //Line 15

System.out.println("Line 16: firstCircle: " + firstCircle ); //Line 16

if (firstCircle.getRadius() > secondCircle.getRadius()) //Line 17
System.out.println("Line 18: The radius of "+ "the first circle is greater than "+ "the radius of the second circle. "); //Line 18
else if (firstCircle.getRadius()< secondCircle.getRadius()) //Line 19
System.out.println("Line 20: The radius of "+ "the first circle is less than the " + "radius of the second circle. "); //Line 20
else //Line 21
System.out.println("Line 22: The radius of " + "both the circles are the same."); //Line 22
}//end main //Line 23
} //Line 24

Sample Sample Output:

Line 10: firstCircle: Radius = 0.00, Perimeter = 0.00, Area = 0.00
Line 11: secondCircle: Radius = 12.00, Perimeter = 75.40, Area = 118.44
Line 12: Enter the radius: 10
Line 16: firstCircle: Radius = 10.00, Perimeter = 62.83, Area = 98.70
Line 20: The radius of the first circle is less than the radius of the second circle.

The preceding program works as follows. The statement in Line 7 creates the object
firstCircle and using the default constructor sets the radius to 0. The statement in
Line 8 creates the object secondCircle and sets the radius to 12. The statement in Line
9 declares the double variable radius. The statement in Line 10 outputs the radius, area,
and perimeter of the firstCircle. Similarly, the statement in Line 11 outputs the
radius, area, and perimeter of the secondCircle The statement in Line 12 prompts the
user to enter the value of radius. The statement in Line 13 stores the value entered by
the user in the variable radius. The statement in Line 15 uses the value of radius to set
the radius of firstCircle. The statement in Line 16 outputs the radius, area, and
perimeter of the firstCircle. The statements in Lines 17 to 23 compare the radius of
firstCircle and secondCircle and output the appropriate result.

Predefined Methods - Java Sample Program

//How to use the predefined methods
import static java.lang.Math.*;
import static java.lang.Character.*;

public class PredefinedMethods
{
public static void main(String[] args)
{
int x;
double u;
double v;

System.out.println("Line 1: Uppercase a is " + toUpperCase('a')); //Line 1

u = 4.2; //Line 2
v = 3.0; //Line 3

System.out.printf("Line 4: %.1f to the power " + "of %.1f = %.2f%n", u, v, pow(u, v)); //Line 4
System.out.printf("Line 5: 5 to the power of " + "4 = %.2f%n", pow(5, 4)); //Line 5

u = u + Math.pow(3, 3); //Line 6

System.out.printf("Line 7: u = %.2f%n", u); //Line 7

x = -15; //Line 8

System.out.printf("Line 9: The absolute value " + "of %d = %d%n", x, abs(x)); //Line 9
}
}


Sample Output:
Line 1: Uppercase a is A
Line 4: 4.2 to the power of 3.0 = 74.09
Line 5: 5 to the power of 4 = 625.00
Line 7: u = 31.20
Line 9: The absolute value of -15 = 15

This program works as follows: The statement in Line 1 outputs the uppercase letter that
corresponds to 'a', which is 'A'. In the statement in Line 4, themethod pow (of the class
Math) is used to output uv. In Java terminology, it is said that the method pow is called with the
(actual) parameters u and v. In this case, the values of u and v are passed to the method pow.

The statement in Line 5 uses the method pow to output 54. The statement in Line 6 uses the method
pow to determine 33, adds this value to the value of u, and then stores the new value into u.
Notice that in this statement, the method pow is called using the name of the class, which is
Math, and the dot operator. The statement in Line 7 outputs the value of u. The statement in
Line 8 stores -15 into x, and the statement in Line 9 outputs the absolute value of x.

Predefined Methods Class Character - Java Sample Program

class Character (Package: java.lang)

Example:

isLowerCase(ch)
ch is of type char. Returns true, if ch is a lowercase
letter; false otherwise.
Example:
isLowerCase('a') returns the value true
isLowerCase('A') returns the value false

isUpperCase(ch)
ch is of type char. Returns true, if ch is an uppercase
letter; false otherwise.
Example:
isUpperCase('B') returns the value true
isUpperCase('k') returns the value false

toLowerCase(ch)
ch is of type char. Returns the character that is the
lowercase equivalent of ch. If ch does not have the
corresponding lowercase letter, it returns ch.
Example:
toLowerCase('D') returns the value d
toLowerCase('*') returns the value *

toUpperCase(ch)
ch is of type char. Returns the character that is the
uppercase equivalent of ch. If ch does not have the
corresponding uppercase letter, it returns ch.
Example:
toUpperCase('j') returns the value J
toUpperCase('8') returns the value 8