JAVA Programming

Support Source Code



JAVA main method and self-instantiation

package coms145;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
import javax.swing.*;

public class NAME

   public NAME();        //constructor
 
   public static void main(String[] args)        //static Main method
       {
        NAME  myInstance = new NAME();
        int rc = myInstance.myProcess(args);
        System.out.println("The Return Code received is " + rc);
        }

    public int myProcess(String[] args)        //non-static method
        {
        statements;
        return 0;
        }

}



JAVA: String to String Array

public String[] stringToArray(String s)  {
    String[] A = new String[s.length()];
    for (int i = 0;  i < s.length();  i++)
        {
        A[i] = s.substring(i,i+1);
        }
    return A;
}


JAVA: CONSOLE Class

/* JAVA
-- Author.  Geoffrey J. Cullen
-- Copyright (c) 1999, 2002 Cullen Programming
-- All Rights Reserved.
--
-- Purpose: Perform Writes and Reads to/from the Console or Terminal.
--
*/

import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.*;
import javax.swing.*;


public class Console
{  
   public static void prompt(String prompt)
   {  System.out.println(prompt);
      System.out.flush();
   }
   public static void write(String prompt)
   {  System.out.println(prompt);
      System.out.flush();
   }
   public static void write(String p1, String p2)
   {  System.out.print(p1 + p2 + '\n');
      System.out.flush();
   }
   public static void write(String p1, String p2, String p3)
   {  System.out.print(p1 + p2 + p3 + '\n');
      System.out.flush();
   }
   public static void write(char x)
   {  System.out.println(x);
      System.out.flush();
   }
   public static void write(char[] x)
   {  System.out.println(x);
      System.out.flush();
   }
   public static void write(int number)
   {  System.out.println(number);
      System.out.flush();
   }
   public static void write(int number1, int number2)
   {  System.out.println(number1 + number2);
      System.out.flush();
   }
   public static void write(String p1, int number)
   {  System.out.println(p1 + number);
      System.out.flush();
   }
    public static void write(String p1, String p2, int number)
   {  System.out.println(p1 + p2 + number);
      System.out.flush();
   }
   public static void write(long number)
   {  System.out.println(number);
      System.out.flush();
   }
    public static void write(long number1, long number2)
   {  System.out.println(number1 + number2);
      System.out.flush();
   }
   public static void write(String p1, long number)
   {  System.out.println(p1 + number);
      System.out.flush();
   }
   public static void write(String p1, String p2, long number)
   {  System.out.println(p1 + p2 + number);
      System.out.flush();
   }
   public static void write(float number)
   {  System.out.println(number);
      System.out.flush();
   }
   public static void write(String p1, float number)
   {  System.out.println(p1 + number);
      System.out.flush();
   }
   public static void write(double number)
   {  System.out.println(number);
      System.out.flush();
   }
   public static void write(String p1, double number)
   {  System.out.println(p1 + number);
      System.out.flush();
   }
   
 
// Read a line from the console. The charstring is terminated by a NEWLINE char.
 public static String consread()
 {
    int ch = 0;
    String str = new String();
    boolean done = false;
    while (!done)
    {
      try
      {
        ch = System.in.read();
        if (ch < 0 || (char) ch == '\n')
        {
           done = true;
        }
        else if ((char)ch != '\r')
        {
           str = str + (char) ch;
        }
      }  // end try
       catch(java.io.IOException e)
       {
          done = true;
       }
    }
    return str;
 }
 
 
   public static int readInt()
   { try
     {
        return Integer.parseInt(consread().trim());
     }
      catch(NumberFormatException e)
      {
         System.out.println("*ERROR. Not an integer.");
         return -1;
      }
   }

    
   public static long readLong()
   { try  
     {
        return Long.parseLong(consread().trim());
     }
      catch(NumberFormatException e)
      {
        System.out.println("*ERROR. Not a long-precision integer.");
        return -1;
      }
   }

    
   public static float readFloat()
   { try
     {
        return Float.parseFloat(consread().trim());
     }
      catch(NumberFormatException e)
      {
        System.out.println("*ERROR. Not a floating point number.");
        return -1;
      }
   }
    

   public static double readDouble()
   { try
     {
       return Double.parseDouble(consread().trim());
     }
      catch(NumberFormatException e)
      {
        System.out.println("*ERROR. Not a double-precision floating point number.");
        return -1;
      }
   }

   public static Object readObject()
   {
     return consread();
   }
 
   public static String ask(String prompt)
   {
     return consread();
   }
   
   public static String readString()
   {
     return consread();
   }
   
   public static String promptString(String prompt)
   {
     String str = JOptionPane.showInputDialog(prompt);
     return str;
   }
   
   public static int promptInt(String prompt)
   {
     String str = JOptionPane.showInputDialog(prompt);
     return Integer.parseInt(str.trim());
   }  
    
   public static String[] parseArgs(String s, String p)
   {
     String patternStr = p;
     String[] fields = s.split(patternStr);
     return fields;
   }  
   
   public static double numformat(double n, int d)
   {
     double dd = Math.round(n * Math.pow(10, d)) / Math.pow(10, d);
     return dd;
   }  
   
   public static String rightJustify(int n, int d)
   {// takes integer and returns a formatted right-justified string of length d.
       String s = Integer.toString(n);
       StringBuffer sb = new StringBuffer(16);
       sb.insert(0,"                ");
       int x = 16 - d + s.length();
       if (x > 16)
       {
           System.out.println("Error, length of input > length of output");
           return "error";
       }
       sb.insert((16-x),s);
       return sb.substring(0, d);
    }  
   
   public static String rightJustify(String s, int d)
   {// takes string and returns a formatted right-justified string of length d.
       StringBuffer sb = new StringBuffer(16);
       sb.insert(0,"                ");
       int x = 16 - d + s.length();
       if (x > 16)
       {
           System.out.println("Error, length of input > length of output");
           return "error";
       }
       sb.insert((16-x),s);
       return sb.substring(0, d);
    }  
   
   public static int pause(int d)
   { // puts thread to sleep for d milliseconds
       try
       {
          Thread.sleep(d);
       }
        catch (InterruptedException e1)
        {
            return 1;
        }
       return 0;
    }  
}



JAVA: Inheritance Example

package javaapplication1;
import javax.swing.*;
import java.util.*;

public class Circle {
  public double radius;

  public Circle() {
    this.radius = 1.0;
  }
 
  public Circle(double radius) {
    setRadius(radius);
  }

  public double getRadius() {
    return radius;
  }

  public void setRadius(double radius) {
    this.radius = radius;
  }

  public double getArea() {
    return Math.PI * Math.pow(radius,2);
  }

  public double getDiameter() {
    return 2 * radius;
  }

  public double getCircumference() {
    return getDiameter() * Math.PI;
  }           
 
  public String toString() {
    return "The circle is created " +
      " and the radius is " + getRadius() +
      ", the area is " + getArea() +     
      ", the circumference is " + getCircumference();
  } 




package javaapplication1;
import javax.swing.*;
import java.util.*;

public class Sphere extends Circle {
  private double area;
  private double volume;

  public Sphere() {
    super();
    this(2.0);
  } 
 
  public Sphere(double radius) {
    super();
    setRadius(radius);
  }

  public double getVolume() {
    volume = (4.0/3.0) * Math.PI * Math.pow(radius,2);
    return volume;
  }
 
  public double getArea() {
    area = super.getArea() * 4;
    return area;
  }

  public String toString() {
    return ("The sphere is created " +
      ", the radius is " + getRadius() +
      ", the area is " + getArea() + 
      ", the circumference is " + getCircumference() +     
      ", the volume is " + getVolume());   
  }
}



package javaapplication1;
import javax.swing.*;
import java.util.*;

public class Cylinder extends Circle {
  private double length;
  private double area;
  private double volume;

  public Cylinder() {
    super();
    this(1.0, 2.0);
  } 
 
  public Cylinder(double radius, double length) {
    super();
    setRadius(radius);
    setLength(length);
  }

  public double getLength() {
    return length;
  }

  public void setLength(double length) {
    this.length = length;
  }
 
  public double getVolume() {
    volume = super.getArea() * length;
    return volume;
  }
 
  public double getArea() {
    area = (2 * super.getArea()) + (length * getCircumference());
    return area;
  }

  public String toString() {
    return ("The cylinder is created " +
      ", the radius is " + getRadius() +    
      ", the length is " + getLength() +
      ", the area is " + getArea() +     
      ", the volume is " + getVolume());
  }
}



package javaapplication1;
import javax.swing.*;
import java.util.*;

public class Cone extends Circle {
  private double height;
  private double area;
  private double volume;

  public Cone() {
    super();
    this(1.0, 2.0);
  } 
 
  public Cone(double radius, double height) {
    super();
    setRadius(radius);
    setHeight(height);
  }

  public double getHeight() {
    return height;
  }

  public void setHeight(double height) {
    this.height = height;
  }
 
  public double getVolume() {
    volume = super.getArea() * height / 3.0;
    return volume;
  }
 
  public double getArea() {
    area = super.getArea() +
    Math.PI * getRadius() *
         Math.sqrt(Math.pow(getRadius(),2) + Math.pow(height,2));
    return area;
  }

  public String toString() {
    return ("The Cone is created " +
      ", the radius is " + getRadius() +    
      ", the height is " + getHeight() +
      ", the area is " + getArea() +     
      ", the volume is " + getVolume());
  }
}



package javaapplication1;
import javax.swing.*;
import java.util.*;

public class Globe extends Sphere {
  private double thickness; 
  private double area;
  private double volume;

  public Globe() {
    super();
    this(2.0,0.1);
  } 
 
  public Globe(double radius, double thickness) {
    super();
    setRadius(radius);
    setThickness(thickness);
  }
 
  public double getThickness() {
    return thickness;
  }

  public void setThickness(double thickness) {
    this.thickness = thickness;
  }
 
  public double getVolume() {
    double x = (4.0/3.0) * Math.PI * Math.pow(radius,2);
    double y = (4.0/3.0) * Math.PI * Math.pow(radius-thickness,2);
    volume = x - y;
    return volume;
  }
 
 
  public String toString() {
    return ("The Globe is created " +
      ", the outer radius is " + getRadius() +
      ", the area is " + getArea() +     
      ", the volume is " + getVolume());   
  }
}




[Return to Professor Page]