3.1 To Declare A Class Circle With Its Center and Radius As The Fields and Two Methods Calculating Area and Circumference
3.1 To Declare A Class Circle With Its Center and Radius As The Fields and Two Methods Calculating Area and Circumference
1 To declare a class Circle with its center and radius as the fields and two methods calculating
area and circumference
class Circle {
double x, y;
double r;
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
}
3.2 To declare a class Circle, create an object and then print the result.
class Circle {
double x, y;
double r;
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
}
class CircleTest {
public static void main(String args[]) {
Circle c1 = new Circle();
c1.x = 1.0;
c1.y = -4.5;
c1.r = 12.36;
c1.area();
c1.circumference();
System.out.println("Area = " + c1.area() + "\t" +
"Circumference= " + c1.circumference());
}
}
3.3 To declare a class Box with its width, height and depth as the fields and two methods
calculating area and volume of it.
class Box {
double width;
double height;
double depth;
double area() {
double a;
a = (width * height + height * depth + width * depth) * 2;
return a;
}
double volume() {
double v;
v = width * height * depth;
return v;
}
}
class Circle {
double x, y;
double r;
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
}
class Box {
double width;
double height;
double depth;
double area() {
double a;
a = (width * height + height * depth + width * depth) * 2;
return a;
}
double volume() {
double v;
v = width * height * depth;
return v;
}
}
class GeoObjects {
public static void main(String args[]) {
Circle c = new Circle();
c.x = 1.0;
c.y = -4.5;
c.r = 12.36;
Box b = new Box();
b.width = 2.0;
b.height = 1.5;
b.depth = 0.5;
c.area();
c.circumference();
b.area();
b.volume();
int the result System.out.println("Circle : Area = " +
c.area() + "\t" + "Circumference = " + c.circumference());
System.out.println("Box : Area = " + b.area() + "\t" +
"Volume = " + b.volume());
}
}
3.5 The following program illustrates the use of nested class in Java programs.
class A {
int x = 555;
void test() {
B b = new B();
b.print();
}
class B {
int y = 111;
void print() {
System.out.println("x = " + x + " and y = " + y);
}
}
}
class NestedClassDemo {
public static void main(String args[]) {
A a = new A();
a.test();
}
}
class Circle {
double x, y;
double r;
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
void setCircle(double a, double b, double c) {
x = a;
y = b;
r = c;
}
}
class ParameterizedMethodDemo {
public static void main(String args[]) {
Circle c1 = new Circle();
Circle c2 = new Circle();
c1.setCircle(3.0, 4.0, 5.0);
c2.setCircle(-4.0, 8.0, 10.0);
System.out.println("Circumference Circle 1" +
c1.circumference());
System.out.println("Area of Circle 1" + c1.area());
System.out.println("Circumference Circle 2" +
c2.circumference());
System.out.println("Area of Circle 2" + c2.area());
}
}
3.7 This program defines the class Circle with a constructor in it.
class Circle {
double x, y;
double r;
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
Circle(double a, double b, double c) {
x = a;
y = b;
r = c;
}
}
class ConstructorDemo {
public static void main(String args[]) {
Circle c1 = new Circle(3.0, 4.0, 5.0);
Circle c2 = new Circle(-4.0, 8.0, 10.0);
System.out.println("Circumference Circle 1" +
c1.circumference());
System.out.println("Area of Circle 1" + c1.area());
System.out.println("Circumference Circle 2" +
c2.circumference());
System.out.println("Area of Circle 2" + c2.area());
}
}
3.8 Example of Constructor overloading: This program defines the class Circle with a multiple
constructors in it.
class Circle {
double x, y;
double r;
static double PI = 3.1432;
double circumference() {
return 2 * PI * r;
}
double area() {
return PI * r * r;
}
Circle() {
System.out.println("Initializing with default values...");
x = 0.0;
y = 0.0;
r = 0.0;
}
Circle(double a, double b, double c) {
x = a;
y = b;
r = c;
}
}
class ConstructorDemo {
public static void main(String args[]) {
Circle c1 = new Circle(); //Call Overloading 1 constructor
Circle c2 = new Circle(-4.0,8.0,10.0);
System.out.println("Circumference Circle 1:" + "\t" +
c1.circumference());
System.out.println("Area of Circle 1:" + "\t" + c1.area());
System.out.println("Circumference Circle 2:" + "\t" +
c2.circumference());
System.out.println("Area of Circle 2:" + "\t" +c2.area());
}
}
3.10 This program illustrates, how signature of the method should be different so that the
method overloading is successful.
class Sum {
void sum(int a, int b) {
System.out.println(a + b);
}
void sum(int a, long b) {
System.out.println(a + b);
}
void sum(int a, int b, int c) {
System.out.println(a + b + c);
}
}
class OverloadingTest1 {
public static void main(String args[]) {
Sum obj = new Sum();
obj.sum(20, 20);
obj.sum(20, 123456789 L);
obj.sum(20, 30, 40);
}
}
3.11 This program illustrate how the ambiguity in type will be resolved during method
overloading in Java.
class TypePromo {
public void show(int x) {
System.out.println("In int" + x);
}
public void show(String s) {
System.out.println("In String" + s);
}
public void show(byte b) {
System.out.println("In byte" + b);
}
}
class OverloadingTest2 {
public static void main(String[] args) {
byte a = 25;
TypePromo obj = new TypePromo();
obj.show(a);
obj.show("Hello");
obj.show(250);
obj.show('A');
3.12 This program shows if difference in return type is sufficient for method overloading.
class Sum {
int sum(int a, int b) {
return a + b;
}
double sum(int a, int b) {
return a + b;
}
}
class OverloadingTest3 {
public static void main(String[] args) {
Sum obj = new Sum();
System.out.println(obj.sum(11, 11));
}
}
class Foo { public static void foo() { System.out.println("Test.foo() called "); } public static void
foo(int a) { System.out.println("Test.foo(int) called "); }}class OverloadingTest4 { Foo obj =
new Foo(); public static void main(String args[]) { obj.foo(); obj.foo(10); } }
class Foo {
public static void main(String[] args) {
System.out.println("I am from normal main... ");
Foo.main("Overloading I");
}
public static void main(String arg1) {
System.out.println("I am from main-I() " + arg1);
Foo.main("God is good", "Joy with Java");
}
public static void main(String a1, String a2) {
System.out.println("Hi, " + a1 + ", " + a2);
}
}
class Circle {
double x, y, r;
static double PI = 3.1432;
double circumference() {
return 2 * PI * r;
}
double area() {
return PI * r * r;
}
}
class StaticVariableTest {
public static void main(String args[]) {
Circle c1 = new Circle();
Circle c2 = new Circle();
c1.x = 1.0;
c2.x = 3.4;
c1.y = -4.5;
c2.y = 2.6;
c1.r = 12.36;
c2.r = 4.9;
c1.area();
c2.area();
c1.circumference();
c2.circumference();
System.out.println("Areas: c1 = " + c1.area() + "\t" + "c2 =
" + c2.area());
System.out.println("Circumferences: c1 = " +
c1.circumference() + "\t" + "c2 = " + c2.circumference());
}
}
3.16 This program illustrates the use of static variable in a program.
}
public Circle(double r) {
this(0.0, 0.0, r);
}
public Circle bigger(Circle c) {
if (c.r > r) return c;
else return this;
}
public static Circle bigger(Circle a, Circle b) {
if (a.r > b.r) return a;
else return b;
}
public static void main(String args[]) {
Circle a = new Circle(2.0);
Circle b = new Circle(3.0);
Circle c = a.bigger(b);
Circle d = Circle.bigger(a, b);
}
}
class StaticBlockDemo {
static int a;
static int b = 555;
static void myMethod(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
a = b - 444;
}
public static void main(String args[]) {
myMethod(96);
myMethod(69);
}
}
3.19 Illustrating the use of this keyword to resolve the name space.
class Circle {
double x, y;
double r;
static double PI = 3.1432;
double circumference() {
return 2 * PI * r;
}
double area() {
return PI * r * r;
}
Circle() {
System.out.println("Initializing with default values...");
x = 0.0;
y = 0.0;
r = 0.0;
}
void setCircle(double a, double b, double c) {
x = a;
y = b; // Set center y-coordinate r = c; }
Circle (double x, double y, double r)
{
this.x = x;
this.y = y;
this.r = r;
}}
class ThisDemo1
{
public static void main(String args[])
{
Circle c1 = new Circle();
c1.setCircle(3.0,4.0,5.0);
Circle c2 = new Circle (-4.0,8.0,10.0);
}}
class Circle {
double x, y;
double r;
Circle(double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
Circle(double r) {
this(0.0, 0.0, r);
}
Circle(Circle c) {
this(c.x, c.y, c.r);
}
Circle() {
this(0.0, 0.0, 1.0);
}
double circumference() {
return 2 * 3.14159 * r;
}
double area() {
return (22 / 7) * r * r;
}
}
class Foo {
int i;
int j;
Foo() {
i = 66;
j = 99;
}
Foo get() {
return this;
}
void print() {
System.out.println("i = " + i + " j = " + j);
}
public static void main(String[] args) {
Foo obj = new Foo();
obj.get().print();
}
}
class Foo {
int i;
int j;
Foo() {
i = 66;
j = 99;
}
Foo get() {
return this;
}
void print() {
System.out.println("i = " + i + " j = " + j);
}
public static void main(String[] args) {
Foo obj = new Foo();
obj.get().print();
}
}
class Foo {
void print() {
this.show();
System.out.println("From print function . . .");
}
void show() {
System.out.println("From show function . . .");
}
public static void main(String args[]) {
Foo f = new Foo();
f.print();
}
}
class A {
B obj;
A(B obj) {
this.obj = obj;
obj.print();
}
}
class B {
int x = 5;
B() {
A obj = new A(this);
}
void print() {
System.out.println("Value of x in Class B : " + x);
}
public static void main(String[] args) {
B obj = new B();
}
}
class Final {
final int ALPHA = 5;
final String PASS;
final float LIMIT;
static final double PI = 3.141592653589793;
static final double N; {
LIMIT = 2.5 f;
}
static {
N = 6.023e23;
}
public Final() {
PASS = "Joy with Java";
}
public static void main(String[] args) {
int array[] = {1,2,3};
for (final int i: array) System.out.print(i + " ");
}
}
import java.lang.*;
class Calculator {
double i;
double x = Math.sqrt(i);
}
class Example {
public static void main(String args[]) {
Calculator a = new Calculator();
a.i = 20;
System.out.println("Square root of " + a.i + " is " + a.x);
}
}
class FormattedPrint {
public static void main(String args[]) {
int x = 100;
System.out.printf("Printing simple integer: x = %d\n", x);
System.out.printf("Formatted with precison: PI = %.2f\n",
Math.PI);
float n = 5.2 f;
System.out.printf("Formatted to specific width: n = %.4f\n",
n);
n = 2324435.3 f;
System.out.printf("Formatted to right margin: n = %20.4f\n",
n);
}
}
3.28 This program will take input through command line arguments and the print the values on
the screen.
class Echo {
public static void main(String args[]) {
for (int i = 0; i < args.length; i++) {
System.out.print(args[i] + " ");
System.out.print("\n");
}
System.exit(0);
}
}
class CommandLineInput1 {
public static void main(String args[]) {
System.out.print("Hi ");
System.out.print(args[0]);
System.out.print(", How are you?");
}
}
3.30 This program demonstrates how a string object can be converted to an integer value.
import java.lang.Math;
class Calculator {
double i, x;
double calsqrt() {
x = Math.sqrt(i);
return x;
}
}
class IntExample {
public static void main(String args[]) {
Calculator a = new Calculator();
a.i = Integer.parseInt(args[0]);
System.out.println("Square root of " + a.i + " is " +
a.calsqrt());
}
}
java IntExample 56
3.31 The following program will take two values from the keyboard and pass the same to a
method to calculate a result.
import java.io.*;
class ReadInput {
static int anyFunction(int x, int y) {
int a = x / y;
return a;
}
public static void main(String args[]) throws IOException {
int a, b, result;
a = 0;
b = 0;
a = System.in.read();
b = System.in.read();
result = anyFunction(a, b);
System.out.println("\nResult : " + result);
}
}
import java.util.Scanner;
class ScannerInput {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
System.out.print("\nEnter your name : ");
String s = scn.nextLine();
System.out.println("\n Hi! " + s);
System.out.print("\nEnter your luck number : ");
int n = scn.nextInt();
System.out.println("\n Is it " + n + "!");
System.out.print("\nWhat is the G value of gravity?");
double g = scn.nextDouble();
System.out.println("God! " + g);
}
}
3.33 The program reads a set of numbers, store them in an array and then calculate the average
value. This program uses ArrayList class defined in java.util package.
import java.util.*;
class AverageOfNumbers {
public static void main(String args[]) {
int sum = 0;
float avg = 0;
ArrayList < Integer > list = new ArrayList < Integer > ();
System.out.println("Enter numbers : Press Ctrl+Z to stop
entering");
Scanner input = new Scanner(System.in);
while (input.hasNextInt()) {
list.add(input.nextInt());
}
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);
}
avg = sum / (list.size());
System.out.println("Average : " + avg);
}
}
import java.io.*;
class BufferInput {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter an integer");
int n = Integer.parseInt(br.readLine());
System.out.println("Enter a String");
String s = br.readLine();
System.out.printf("You have entered: " + n + " and " + s);
}
}
import java.io.*;
class InterestCalculator {
public static void main(String args[]) {
Float principalAmount = new Float(0);
Float rateOfInterest = new Float(0);
int numberOfYears = 0;
try {
DataInputStream in = new DataInputStream(System.in);
String tempString;
System.out.println("Enter Principal Amount: ");
System.out.flush();
tempString = in .readLine();
principalAmount = Float.valueOf(tempString);
System.out.println("Enter Rate of Interest: ");
System.out.flush();
tempString = in .readLine();
rateOfInterest = Float.valueOf(tempString);
System.out.println("Enter number of years: ");
System.out.flush();
tempString = in .readLine();
numberOfYears = Integer.parseInt(tempString);
} catch (Exception e) {}
Float interest = principalAmount * rateOfInterest *
numberOfYears / 100;
System.out.println("Total Interest = " + interest);
}
}
class Fibonacci {
static int n;
static int fibonacci(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return (fibonacci(n - 1) + fibonacci(n - 2));
}
public static void main(String args[]) {
n = Integer.parseInt(args[0]);
for (int i = 0; i <= n; i++) {
System.out.println(fibonacci(i));
}
}
}
3.41 This Java program demonstrates the recursive method to solve Tower of Hanoi puzzle.
class TowerOfHanoi {
static void move(int n, char a, char c, char b) {
if (n == 1) {
System.out.println("Move disk 1 from Peg " + a + " C
" + c);
return;
}
move(n - 1, a, b, c);
System.out.println("Move disk " + n + " from " + a + " to
" + c);
move(n - 1, b, c, a);
}
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
move(n, 'A', 'C', 'B');
}
}
OUTPUT:
3Move disk 1 from Peg A C CMove disk 2 from A to BMove disk 1 from Peg C C BMove disk 3
from A to CMove disk 1 from Peg B C AMove disk 2 from B to CMove disk 1 from Peg A C C