Home Listings By Name By Subject Email www.tomswan.com Help |
© 2001 by Tom Swan. All rights reserved.
Updated: 6/4/01 12:20:54 PM
Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page
001: // Declare DateClass 002: class DateClass { 003: int month; 004: int day; 005: int year; 006: public DateClass(int m, int d, int y) { 007: month = m; 008: day = d; 009: year = y; 010: // year = y + 1900; 011: } 012: public void display() { 013: System.out.println(month + "/" + day + "/" + year); 014: } 015: } 016: 017: // Declare main program class 018: class DateObject { 019: public static void main(String args[]) { 020: // Create and display a DateClass object 021: DateClass birthday = new DateClass(7, 18, 64); 022: birthday.display(); 023: // Create and display another DateClass object 024: DateClass future = new DateClass(1, 1, 01); 025: // DateClass future = new DateClass(1, 1, 101); 026: future.display(); 027: } 028: }Return to top
001: import java.util.Date; // Import the Date class 002: 003: // Use the imported Date class 004: class DateDemo { 005: public static void main(String args[]) { 006: Date today = new Date(); 007: System.out.println(today.toString()); 008: } 009: }Return to top
001: import java.util.Date; // Import the Date class 002: 003: // Extend the imported Date class 004: class NewDate extends Date { 005: public void display() { 006: System.out.println(toString()); 007: } 008: } 009: 010: // Use the NewDate class 011: class DateShow { 012: public static void main(String args[]) { 013: NewDate today = new NewDate(); // Construct NewDate object 014: today.display(); // Call the new display() method 015: } 016: }Return to top
001: // Method demonstration class 002: class MethodClass { 003: int sum(int a, int b, int c) { 004: return a + b + c; 005: } 006: double product(double x, double y) { 007: return x * y; 008: } 009: void showErrorMessage(int code) { 010: switch (code) { 011: case 1: 012: System.out.println("Error 1: Deep trouble!"); 013: break; 014: case 2: 015: System.out.println("Error 2: Deeper trouble!"); 016: break; 017: default: 018: System.out.println("Unknown code: Situation hopeless"); 019: } 020: } 021: } 022: 023: // Main program class 024: class Methods { 025: public static void main(String args[]) { 026: // Create demo object of the MethodClass class 027: MethodClass demo = new MethodClass(); 028: 029: // Call demo object's sum() method 030: int k = demo.sum(10, 25, 16); 031: System.out.println("sum = " + k); 032: 033: // Call demo object's product() method 034: double f = demo.product(3.14159, 4.5); 035: System.out.println("product = " + f); 036: 037: // Call demo object's showErrorMessage() method 038: demo.showErrorMessage(1); 039: demo.showErrorMessage(2); 040: } 041: }Return to top
001: class Serialized { 002: static private int nextSerialNum; // Initialized to 0 003: private int serialNum; 004: // Construct a Serialized object 005: Serialized() { 006: // Increment and assign serial number to an object 007: serialNum = ++nextSerialNum; 008: } 009: // Show the object's serial number 010: public void showSerialNumber(String name) { 011: System.out.println(name + "'s serial number = " + serialNum); 012: } 013: } 014: 015: class Serial { 016: public static void main(String args[]) { 017: Serialized obj1 = new Serialized(); 018: Serialized obj2 = new Serialized(); 019: Serialized obj3 = new Serialized(); 020: obj1.showSerialNumber("Object 1"); 021: obj2.showSerialNumber("Object 2"); 022: obj3.showSerialNumber("Object 3"); 023: } 024: }Return to top
001: class DemoClass { 002: // Method #1 003: void show(int x) { 004: System.out.println("int x = " + x); 005: } 006: // Method #2 007: void show(double x) { 008: System.out.println("double x = " + x); 009: } 010: // Method #3 011: void show(char x) { 012: System.out.println("char x = " + x); 013: } 014: } 015: 016: class Overload { 017: public static void main(String args[]) { 018: DemoClass myObj = new DemoClass(); // Create object 019: myObj.show(123); // Call show() #1 020: myObj.show(3.14159); // Call show() #2 021: myObj.show('Q'); // Call show() #3 022: } 023: }Return to top
001: class OutputDemo { 002: public static void main(String args[]) { 003: StringBuffer s = new StringBuffer(); 004: for (char c = 'A'; c <= 'Z'; c++) { 005: s.append(c); 006: } 007: System.out.println(s); // Displays the alphabet 008: } 009: }Return to top
001: import java.io.IOException; 002: 003: class InputDemo { 004: public static void main(String args[]) { 005: try { 006: // Input a single character 007: System.out.println("Type a character:"); 008: char ch = (char)System.in.read(); 009: System.out.println("You entered: " + ch); 010: // Throw out new line 011: while (ch != '/n') 012: ch = (char)System.in.read(); 013: // Input a string 014: System.out.println("Type a string:"); 015: StringBuffer s = new StringBuffer(); 016: while ((ch = (char)System.in.read()) != '/n') 017: s.append(ch); 018: System.out.println("You entered: " + s); 019: } catch (IOException e) { 020: System.out.println("Input error detected"); 021: } 022: } 023: }Return to top
001: class AnyClass { 002: AnyClass() { 003: System.out.println("Inside AnyClass() constructor"); 004: } 005: protected void finalize() { 006: System.out.println("Inside AnyClass() finalize method"); 007: } 008: } 009: 010: class FinalDemo { 011: public static void f() { 012: System.out.println("Start method f()"); 013: AnyClass obj1 = new AnyClass(); 014: System.out.println("End method f()"); 015: } 016: public static void main(String args[]) { 017: System.out.println("Start method main()"); 018: f(); 019: AnyClass obj2 = new AnyClass(); 020: System.out.println("End method main()"); 021: } 022: }Return to top