0% found this document useful (0 votes)
35 views16 pages

Mod 3

The document contains code for several Java classes and methods. It defines classes like Student, Circle, TV, and methods like max(), hexToDecimal(), createCircleArray(). The code shows how to define classes with constructors, fields, and methods in Java as well as how to instantiate objects and call methods.

Uploaded by

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

Mod 3

The document contains code for several Java classes and methods. It defines classes like Student, Circle, TV, and methods like max(), hexToDecimal(), createCircleArray(). The code shows how to define classes with constructors, fields, and methods in Java as well as how to instantiate objects and call methods.

Uploaded by

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

public static int max (int numl, int num2) {

int

int result;

if (numl > num2)

result numl;

else

result = num2;

return result;

import java.util.Scanner;

public class Hex2Dec {

/** Main method */

public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in);

// Prompt the user to enter a string System.out.print ("Enter a hex number: "); String hex =
input.nextLine();

System.out.println("The decimal value for hex number "

+hex+" is " + hexToDecimal (hex.toUpperCase()));


}

public static int hexToDecimal (String hex) {

int decimalValue = 0; for (int i = 0; i < hex.length(); i++) { char hexChar hex.charAt(i):

decimalValue = decimal Value 16+ hexCharToDecimal (hexChar):

return decimalValue; }

public static int hexCharToDecimal (char ch) { if (ch >= 'A' && ch <= 'F') return 10+ ch - 'A'; else // ch is '0',
'1', .... or '9' return ch '0';

public class TestMethodOverloading { /* Main method */ public static void main(String[] args) {

// Invoke the max method with int parameters

System.out.println("The maximum of 3 and 4 is "

+ max (3, 4));

//Invoke the max method with the double parameters


System.out.println("The maximum of 3.0 and 5.4 is " max(3.0, 5.4)); // Invoke the max method with
three double parameters System.out.println("The maximum of 3.0, 5.4, and 10.14 is "

+max(3.0, 5.4, 10.14));

/ Return the max of two int values "/ public static int max(int numl, int num2) { if (num1> num2) return
numl;

else

return num2:

public static double max (double num1, double num2) {

if (num1> num2) return numl;

else

return num2;

/* Return the max of three double values / public static double max (double numl, double num2, double
num3) { return max (max (num1, num2), num3); }

public class TestSimpleCircle { /** Main method */


public static void main(String[] args) { // Create a circle with radius 1 SimpleCircle circle1 = new
SimpleCircle(); System.out.println("The area of the circle of radius" + circlel. radius +" is " +
circle1.getArea());

// Create a circle with radius 25

SimpleCircle circle2 = new SimpleCircle(25);

System.out.println("The area of the circle of radius " + circle2. radius +"is" + circle2.getArea()); // Create
a circle with radius 125 SimpleCircle circle3 = new SimpleCircle(125);

System.out.println("The area of the circle of radius"

+ circle3.radius +" is " + circle3.getArea());

Modify circle radius circle2. radius = 100; // or circle2.setRadius(100) System.out.println("The area of the
circle of radius" + circle2. radius" is " + circle2.getArea()):

// Define the circle class with two constructors

class SimpleCircle {

double radius:

View

need to edit, it's safer to stay in Protected View

/* Construct a circle with radius 1 * SimpleCircle() { radius = 1;


}

no-arg c

Construct a circle with a specified radius */ SimpleCircle(double newRadius) { radius = newRadius:

second co

Return the area of this circle */ double getArea() { return radius radius Math.PI:

getArea

Return the perimeter of this circle */ double getPerimeter() { return 2 radius Math.PI;

getPerime

/* Set a new radius for this circle "/ void setRadius(double newRadius) { radius newRadius;

public class TV {

int channel = 1; // Default channel is 1 int volumeLevel = 1; // Default volume level is 1 boolean on =
false; // TV is off

public TV() { }
public void turnOn() { on = true;

public void turnoff() {

on = false; }

public void setChannel (int newChannel) { if (on && newChannel >= 1 && newChannel <=

channel = newChannel;

public void setVolume (int newVolumeLevel) { if (on && newVolumeLevel >= 1 && newVolumeLevel <=
7) volumeLevel = newVolumeLevel;

public void volumeUp() { if (on && volumeLevel < 7) volumeLevel++;

public void volumeDown() { if (on && volumeLevel > 1) volumeLevel--

public class TestTV { public static void main(String[] args) {

TV tv1 = new TV(); tv1.turnOn();


tvl.setChannel (30); tvl.setVolume (3);

TV tv2 = new TV();

tv2.turnOn(); tv2.channel Up():

tv2.channelUp(): tv2.volumeUp();

System.out.println("tvl's channel is " + tv1.channel +" and volume level is " + tv1.volumelevel):
System.out.println("tv2's channel is tv2.channel and volume level is "+tv2.volumeLevel);

class Student5{

int id;

String name;

int age;

//creating two arg constructor

Student5(int i,String n){

id = i;

name = n; }
//creating three arg constructor

Student5(int i,String n,int a){

id = i;

name = n; age=a; )

void display()(System.out.println(id+" "+name+" "+age):)

public static void main(String args[])

Student5 $1 = new Student5(111,"Karan"); Student5 s2= new Student5(222,"Aryan",25);

s1.display();

s2.display();

public class DestructorExample

public static void main(String[] args) {

de = null; System.gc();

DestructorExample de = new DestructorExample (); de.finalize(); System.out.println("Inside the main()


method"); }
protected void finalize() {

System.out.println("Object is destroyed GC"); }

public class Test {

public static void main(String[] args) {

Student student = new Student();

System.out.println("name?

" + student.name);

System.out.println("age? " + student.age);

System.out.println("isScienceMajor? "student.isScienceMajor):

System.out.println("gender? " + student.gender);

import java.util.Date; public class Date Demo (

public static void main(String args[]) {

//Instantiate a Date object Date date new Date();


// display time and date using toString() System.out.println(date.toString());

Random random1 = new Random (3);

System.out.print ("From randoml: ");

for (int i = 0; i < 10; i++) System.out.print (random1.nextInt (1000) + " ");

Random random2 = new Random (3);

System.out.print ("\nFrom random2: ");

for (int i = 0; i < 10; i++)

System.out.print (random2.nextInt (1000)+" ");

import java.util.Random:

public class JavaRandomExample1 { public static void main(String[] args) {

//create random object

Random random= new Random();

//returns unlimited stream of pseudorandom long values

System.out.println("Longs value: "+random.longs());

// Returns the next pseudorandom boolean value


boolean val = random.nextBoolean();

System.out.println("Random boolean value : "+val);

byte[] bytes = new byte[10];

//generates random bytes and put them in an array random.nextBytes(bytes);

System.out.print("Random bytes = (");

for(int i=0; i< bytes.length; i++)

System.out.printf("%d", bytes[i])

System.out.print(")");

import java.util.Scanner;

import javafx.geometry. Point2D; {

public class TestPoint2D public

static void main(String[] args) { = new Scanner(System.in);


Scanner input

System.out.print("Enter point1's x-, y-coordinates: ");

double x1 = input.nextDouble(): double y1= input.nextDouble();

System.out.print("Enter point2's x-, y-coordinates: "); double x2 = input.nextDouble():

double y2= input.nextDouble():

Point2D pl = new Point2D (x1,y1): Point2D p2 = new Point2D(x2, y2):

System.out.println("pl is "+pl.toString());

System.out.println("p2 is "+ p2.toString()); System.out.println("The distance between pl and p2 is '

pl.distance (p2));

import java.util.*;

public class BlockExample{ // static variable

static int j = 10;

static int n;

// static block
static {

System.out.println("Static block initialized.");

n = j * 8;

public static void main(String[] args)

System.out.println("Inside main method"); System.out.println("Value of j: "+j); System.out.println("Value


of n: "+n);

import java.util.*; public class VariableExample

// static variable

static int j = n(); // static block

static {

System.out.println("Inside the static block");

// static method static int n() {

System.out.println("from n "); return 20;


}

// static method(main !!) public static void main(String[] args)

System.out.println("Value of j: "+j); System.out.println("Inside main method");

public class SMEC {

private static String str = "Welcome SMEC-VIT";

// Static class

static class MyNestedClass {

// non-static method

public void disp({

System.out.println(str);

public static void main(String args[])

SMEC.MyNestedClass obj - new SMEC.MyNestedClass(); obj.disp();

public class TotalArea { /** Main method */


public static void main(String[] args) {

//Declare circleArray

CircleWithPrivateDataFields[] circleArray;

// Create circleArray circleArray createCircleArray();

// Print circleArray and total areas of the circles

printCircleArray(circleArray);

/** Create an array of Circle objects */

public static CircleWithPrivateDataFields[] createCircleArray()) { CircleWithPrivateDataFields[] circleArray-


new CircleWithPrivateDataFields[5]:

for (int i=0; i<circleArray.length; i++) {

circleArray[i]- new CircleWithPrivateDataFields(Math.random() * 100);

// Return Circle array

return circleArray;

/** Print an array of circles and their total area */ public static void printCircleArray()

CircleWithPrivateDataFields[] circleArray) ( System.out.printf("%-30s%-15s\n", "Radius", "Area");


for (int i=0; i<circleArray.length; i++) { System.out.printf("%-30%-15f\n", circleArray[i].getRadius().

circleAmay[i].getArea());

System.out.println("---

-");

// Compute and display the result System.out.printf("%-30%-15f\n", "The total areas of circles is",

sum(circleArray)):

** Add circle areas */

public static double sum(

Circle WithPrivateDataFields[] circleArray) {

// Initialize sum double sum=0;

Il Add areas to sum

for (int i=0; i<circleArray.length; i++)

sum += circleArray[i].getArea();

return sum;

You might also like