Friday, March 7, 2014

Code Examples

Shown here are a few examples of my computer coding work. I took COP3502 and COP3503 so I have a basic understanding of the Java programming language. I also taught myself some basic HTML as i mentioned in an earlier post.


Construction of a Circle Object using Java
Constructs a simple circle and returns basic values about the circle when prompted (such as radius, etc)


public class Circle
{
    private int radius;
    private String color;
    private boolean isVisible;
    private int x;

public Circle()
    {
        // radius = 2;
        color = "blue";
        isVisible = true;
   
        x = 0;
    }
public Circle(int r, String c, boolean v) {
radius = r;
color = c;
isVisible = v;
}
   public int getRadius()
    {

        return radius;
    }
    public void setRadius(int r) {
        radius = r;
    }
 
}

-----------------------------------------------------------------------------------------

Basic String Test/Return Program using Java
Uses imported method called "noSpaces"


#include "nospaces.h"

int main(){
  char test[256];

  std::cout << "Please enter a string" << std::endl;

  std::cin.getline(test, 256);

  char * c = noSpaces(test);

  std::cout << "Original: " << test << std::endl << "No Spaces: " << c << std::endl;

  delete [] c;

  return 0;
}

------------------------------------------------------------------------------------

Basic Recursion Programming using Java
Also uses imported method called "noSpaces"


#include "nospaces.h"

int lengthNoSpaces(char const * const str){
  if(*str == '\0'){
    return 0;
  }

  if(*str != ' '){
    return 1 + lengthNoSpaces(str+1);
  }

  return lengthNoSpaces(str+1);
}

void fillIn(char * const copy, char const * const original){
  if(*original == '\0'){
    *copy = '\0';
    return;
  }

  if(*original != ' '){
    *copy = *original;
    fillIn(copy+1, original+1);
    return;
  }

  fillIn(copy, original+1);
}

char * noSpaces(char const * const str){
  int n = lengthNoSpaces(str);
  char * copy = new char[n+1];

  fillIn(copy, str);
  
  return copy;
}

 

No comments:

Post a Comment