Professional Documents
Culture Documents
int
int result;
result numl;
else
result = num2;
return result;
import java.util.Scanner;
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();
int decimalValue = 0; for (int i = 0; i < hex.length(); i++) { char hexChar hex.charAt(i):
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) {
/ Return the max of two int values "/ public static int max(int numl, int num2) { if (num1> num2) return
numl;
else
return num2:
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); }
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);
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()):
class SimpleCircle {
double radius:
View
no-arg c
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;
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;
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;
id = i;
name = n; }
//creating three arg constructor
id = i;
name = n; age=a; )
s1.display();
s2.display();
de = null; System.gc();
System.out.println("name?
" + student.name);
System.out.println("isScienceMajor? "student.isScienceMajor):
for (int i = 0; i < 10; i++) System.out.print (random1.nextInt (1000) + " ");
import java.util.Random:
System.out.printf("%d", bytes[i])
System.out.print(")");
import java.util.Scanner;
System.out.println("pl is "+pl.toString());
pl.distance (p2));
import java.util.*;
static int n;
// static block
static {
n = j * 8;
// static variable
static {
// Static class
// non-static method
System.out.println(str);
//Declare circleArray
CircleWithPrivateDataFields[] circleArray;
printCircleArray(circleArray);
return circleArray;
/** Print an array of circles and their total area */ public static void printCircleArray()
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)):
sum += circleArray[i].getArea();
return sum;