Escolar Documentos
Profissional Documentos
Cultura Documentos
*/
class DefaultInit {
byte b;
short s;
int i;
long l;
float f;
double d;
boolean bool;
String str;
public static void main(String[] args) {
// Create an object of DefaultInit class
DefaultInit obj = new DefaultInit();
// Print the default values for all instance variables
System.out.println("byte is initialized to " + obj.l);
System.out.println("short is initialized to " + obj.s);
System.out.println("int is initialized to " + obj.i);
System.out.println("long is initialized to " + obj.l);
System.out.println("float is initialized to " + obj.f);
System.out.println("double is initialized to " + obj.d);
System.out.println("boolean is initialized to " + obj.bool);
System.out.println("String is initialized to " + obj.str);
}
}
/*
byte is initialized to 0
short is initialized to 0
int is initialized to 0
long is initialized to 0
float is initialized to 0.0
double is initialized to 0.0
boolean is initialized to false
String is initialized to null
*/
/* Declaration of a Human Class with One Class Variable and Two Instance Variables. Human.java
*/
public class
String
String
static
}
Human {
name;
gender;
long count;
// Instance variable
// Instance variable
// Class variable
/* Here, Box defines three constructors to initialize the dimensions of a box various
ways. - OverloadCons.java
*/
class Box {
double width;
double height;
double depth;
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
class OverloadCons {
public static void main(String args[]) {
// create boxes using the various constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println("Volume of mycube is " + vol);
}
}
/*
Volume of mybox1 is 3000.0
Volume of mybox2 is -1.0
Volume of mycube is 343.0
*/
/* Accessing Class Fields from Static and Non-static Methods. Examples of Invoking
Instance Methods and Class Methods of a Class. - MethodType.java */
public class MethodType {
5
// MethodTypeTest.java
public class MethodTypeTest {
public static void main(String[] args) {
// Create an instance of the MethodTYpe class
MethodType mt = new MethodType();
System.out.println("Invoking instance method...");
// Invoke the instance method
mt.printMN();
System.out.println("Invoking class method on class name...");
// Invoke the class method using the class name
MethodType.printM();
System.out.println("Invoking class method on an instance...");
// Invoke the class method using the instance reference
mt.printM();
}
}/*
Invoking instance method...
printMN() - m = 100
printMN() - n = 200
Invoking class method on class name...
printM() - m = 100
Invoking class method on an instance...
printM() - m = 100
*/
/* A MainTest2 Class, Which Declares a main( ) Method, Which in Turn Calls the main()
Method of the MainTest1 Class - MainTest2.java */
public class MainTest2 {
public static void main(String[] args) {
MainTest1.main(args);
}
}
// run MainTest2
/* Inside main() method of the MainTest1 class */
Or
// run MainTest1
/* Inside main() method of the MainTest1 class */
---------------------------------------------------------------------------------------------
}
// Push an item onto the stack
void push(struct stack *s, int item) {
if(s->tos==MAX-1)
printf("Stack is full.\n");
else
s->stck[++s->tos] = item;
}
// Pop an item from the stack
int pop(struct stack *s) {
6
}
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++) mystack1.push(i);
for(int i=10; i<20; i++) mystack2.push(i);
// pop those numbers off the stack
System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
/*
C:\java-class-programs>javac TestStack.java
C:\java-class-programs>java TestStack
Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16
15
14
13
12
11
10
*/
// StringLength.java
public class StringLength {
public static void main (String[] args) {
// Create two string objects
String str1 = new String() ;
String str2 = new String("Hello") ;
// Get the length of str1 and str2
int len1 = str1.length();
int len2 = str2.length();
// Display the length of str1 and str2
System.out.println("Length of \"" +
str1 + "\" = " + len1);
System.out.println("Length of \"" +
str2 + "\" = " + len2);
}
}
/*
C:\java-class-programs>javac StringLength.java
C:\java-class-programs>java StringLength
Length of "" = 0
Length of "Hello" = 5
*/