Thursday, July 15, 2010

package org.cs212.hello;
public class RoadBike extends Bicycle{
      private int tireWidth; // In millimeters (mm)

      public RoadBike(int startCadence, int startSpeed, int startGear, int newTireWidth){
        super(startCadence, startSpeed, startGear);
        this.setTireWidth(newTireWidth);
      }

      public int getTireWidth(){
        return this.tireWidth;
      }

      public void setTireWidth(int newTireWidth){
        this.tireWidth = newTireWidth;
      }

      public void printDescription(){
        super.printDescription();
        System.out.println("The RoadBike has " + getTireWidth()
                + " MM tires.");
      }
}
package org.cs212.hello;
public class MountainBike extends Bicycle{
      private String suspension;
      public double name;
     
      public MountainBike(int startCadence, int startSpeed, int startGear, String suspensionType){
        super(startCadence, startSpeed, startGear);
        this.setSuspension(suspensionType);
      }

      public String getSuspension(){
        return this.suspension;
      }

      public void setSuspension(String suspensionType){
        this.suspension = suspensionType;
      }

      public void printDescription(){
        super.printDescription();
        System.out.println("The MountainBike has a " + getSuspension()
                + " suspension.");
      }
    }
package org.cs212.hello;

public class Bicycle {
   
    // the Bicycle class has three fields
    public int cadence;
    public int gear;
    public int speed;
    public int name;
   
    // the Bicycle class has one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
   
    // the Bicycle class has four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
   
    public void printDescription()
    {
         System.out.println("\nBike is in gear " + this.gear + " with a cadence of " +
                this.cadence + " and travelling at a speed of " + this.speed + ". ");
    }

   
    public void setGear(int newValue) {
        gear = newValue;
    }
   
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
   
    public void speedUp(int increment) {
        speed += increment;
    }

}
package org.cs212.hello;

import java.util.ArrayList;

public class MyLibrary {
    ArrayList<Book> books;
    ArrayList<Person> people;
    String name;
   
    public MyLibrary(String n)
    {
        name = n;
        books = new ArrayList<Book>();
        people = new ArrayList<Person>();
    }

    public ArrayList<Book> getBooks() {
        return books;
    }

    public ArrayList<Person> getPeople() {
        return people;
    }

    public String getName() {
        return name;
    }
   
    public void addBook(Book b)
    {
        books.add(b);
    }
   
    public void addPerson(Person p)
    {
        people.add(p);
    }
   
    public void removePerson(Person p)
    {
        people.remove(p);
    }
   
   
    public void removeBook(Book b)
    {
        books.remove(b);
    }
   
    boolean checkOut(Book b, Person p)
    {
        if (b.getPerson() == null &&
                getBooksForPerson(p).size() == p.getMaximumBooks())
        {
            b.setPerson(p);
            return true;
        }
        else
        {
            return false;
        }
    }
   
    boolean checkIn(Book b)
    {
        if (b.getPerson() != null)
        {
            b.setPerson(null);
            return true;
        }
        else
        {
            return false;
        }
    }
   
    ArrayList<Book> getBooksForPerson(Person p)
    {
        ArrayList<Book> result = new ArrayList<Book>();
       
        for (Book x : books)
        {
            if (x.getPerson() != null &&
                x.getPerson().getName().equals(p.getName())
               )
                result.add(x);
        }
       
        return result;
    }
   
}
package org.cs212.hello;

public class Person
{
    // fields
    private String name; // the person's name
    private int maximumBooks;
  
    // constructor
    public Person()
    {
        name = "unknown name";
        maximumBooks = 3;
    }
   
    // getters and setters
    public String getName()
    {
        return name;
    }
  
    public void setName(String n)
    {
        name = n;
    }

    public int getMaximumBooks() {
        return maximumBooks;
    }

    public void setMaximumBooks(int maxBooks) {
        maximumBooks = maxBooks;
    }
   
    public String toString()
    {
        return getName() + " (" + getMaximumBooks() + " books)";
    }
  
}
package org.cs212.hello;

public class Book {

    String title;
    String author;
    Person person;

    public Book(String string) {
        this.title = string;
        this.author = "unknown author";
    }

    public void setPerson(Person p) {
        person = p;
    }

    public Person getPerson() {
        return person;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

}
package mylinkedlist;

public class MyMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        LinkedList<Integer> myLL = new LinkedList<Integer>();
        System.out.println(myLL.isEmpty());
        if (! myLL.isEmpty())
            System.out.println(myLL.front());
        myLL.addAtBeginning(5);
        myLL.addAtBeginning(6);
        myLL.addAtBeginning(15);
        myLL.printAllItems();
        System.out.println(myLL.front());
    }

}