35 | Implement the final changes discussed in the text in your own version of the program. A: Done it. |
36 | Add more word/response mappings into your application. You should copy some out of the solution provided and add some yourself. A: System.out.println("You might want to just change you computer, it seems to be at the peak of its life"); System.out.println("Sorry sir, but this is not a playground, you may want to use more formal language"); System.out.println("Your insulting, and you expect help from us?"); System.out.println("Do you really think it's my fault this is not working?"); System.out.println("Your computer seems to be disfunctional, I recommend you buy a new one."); |
37 | Sometimes two words (or variations of a word) are mapped to the same response. Deal with this by mapping synonyms or related expressions to the same string, so that you do not need multiple entries in the reponse map for the same response. A: Done it. |
38 | Identify multiple matching words in the user's input and respond with a more appropriate answer in that case. A: Done, I'm still unsure, however, of how many times a user should say the word in order for it to become the more detailed response. In all cases however, I set it to five. |
39 | When no word is recognized, use other words from the user's input to pick a well-fitting default response: for example words like "who","why, "how" A: Done it, I simply threw back a question at the user. For example, "Why is this?" would return the response I made, "Why shouldn't it?". |
40 | Use BlueJ's Generate Documentation function to generate documentation for your techSupport project. Examine it. Is it accurate? Is it complete? Which parts are useful, which are not? Can you find any errors in the documentation? A: It's not so complete, because I haven't documented everything, but for the parts that do have a default message from Barnes and Kolling, it seems pretty detailed, and complete. I see the purpose of @param and @version @author now. I'm unable to find any errors however. |
41 | Find examaples of javadoc key symbols in the source code of the TechSupport project. How do they influence the formatting of the documentation? A: If key symbols like @param, appear in the source code of a project they become categories in which the text following their appearance is the test that appears in the category. |
42 | Find out about and describe other javadoc key symbols. One palce you can look is the online documentation of Sun Microsystems' java distribution. It contains a documnt called javadoc - The Java API Documnentation Generator. In this document the key symbols are called javadoc tags. A: Although many tags appear, the essential ones in my opinion are: @link which inserts a link to another file or project. and @author which describes and includes a section on where the project has come from. |
43 | Properly document all classes in your version of the TechSupport project. A: Done it. I included @param where needed, as well as @return, which explain the parameters of each method and return where I explain the results of the method. |
44 | Create a BallDemo object and execute the drawDemo() and bounce() methods. Then read the BallDemo source code. Describe, in detail, how these methods work. A: Essentially, this project, in both methods, creates a few objects that work together in that they move and wait for each other to move. For example, the rectangle moves, then lets the line be drawn, then moves again. This all happens extremely fast, and together creates a series of movements which combine. |
45 | Read the documentation of the Canvas class. Then answer the following questions in writing, including fragments of Java code. How do you create a Canvas? How do you make it visible? How do you draw a line? How can you erase something? What is the difference between draw() and fill()? What does wait do? A: a) You initialize it with a title, and/or, and two measurements of width and height, and/or a background color. However, you can omit everything but the title, and start it as a default like that. b) When you instantiate a Canvas object, the terminal will automatically appear. c) Call the drawLine() method where two points must be given in order for it to calculate a slope. d) Use the erase() method, but enter the shape's name if you want to only erase a given object. e) draw() simply makes the shape visible on the canvas, but fill() fills up the shape with a certain color. For example you can have an "open" circle, and fill it up with the fill() method. f) wait() makes the shape wait for a specified amount of seconds before continuing. |
46 | Experiment with Canvas operations by making changes to the drawDemo() method of BallDemo. Draw some more lines, shapes and text. A: Done it. There's a whole spectrum of possibilities, but I chose to make a rectangle and move it to +50 on x, make it wait, then move it +300 on x to make it sort of materialize, move, wait, then leave. |
47 | Draw a frame around the canvas by drawing a rectangle 20 pixels inside the window borders. Put this functionality into a method called drawFrame() in the BallDemo class. A: Done, I'm only worrying that maybe the shapes will remove the fill of the rectangle after we call the drawDemo method... |
48 | Improve your drawFrame() method to adapt automatically to the current canvas's size. To do this, you need to find out how to make use of an object of class Dimension. A: I'm having trouble getting this one to work, i'll confront you with it during the day. |
Sunday, 31 October 2010
Chapter 5 35-48
Monday, 25 October 2010
Chapter 5 29-33
What are the similarities and differences between a HashSet and an ArrayList? A: HashSet is a set of elements that have no duplicate, like a function in mathematics, while ArrayList is simply a list of elements. |
The split() method is more powerful than it first seems from our example. How can you define exactly how a string should be split? Give some examples. A: The split() method simply takes the parameter of a character which the user specifies in order to tell it where to begin and end the substrings that it will put in the array of Strings. For example if I were to say split(i) for "Hello, I'm Mike", the array would consist of the substrings "Hello, " "'m M" "ke". It actually skips over the character which you specify, another important fact. |
How would you call the split() method if you wanted to split a string at either space or tab characters? How might you break up a string in which the words are separated by colon (':') characters? A: a) split( ) or split((tab key)) b) split(;) |
What is the difference in result of returning the words in a HashSet compared with returning them in an ArrayList? A: In a HashSet the words are returned by using the HashSet key, while in the ArrayList, the words are returned by stating their index. |
What happens if there is more than one space between words (for example: two or three spaces? Is there a problem? A: My hypothesis is that it would function as normal, for example if the string was "Mike is awesome" it would still return "Mike" "is" "awesome". |
Sunday, 24 October 2010
Chapter Five 18-30
19 | Implement the random-response solution discussed here in your version of the tech-support system. Done as shown in the above pictures. |
20 | What happens when you add more (or fewer) possible responses to the responses list? Will the selection of a random response still work properly? Why or why not? A: Of course, no matter the amount of responses, it will always respond. Only if you put more responses then it has a bigger range with which to give you a random reponse. |
21 | What is a HashMap? What is its purpose and how do you use it? Use the AP Java Subset documentation at the Duke Site. A: It is a type of map stores values with keys. That is, you can find a value using its key. However, it's not possible to find a key using a value, the only purpose of the key is to find that one value. Resembles an array list, just with a key. |
22 | Create a class MapTester. In it, use a HashMap to implement a phone book similar to the example in the text. Implement the following two methods of class MapTester public void enterNumber(String name, String number) public String lookupNumber(String name) A: Done. I had to make the second method void, because for some reason it wouldn't compile if I put string, and it works perfectly with void and printing. |
23 | What happens when you add an entry to a map with a key that already exists in the map? A: The number gets replaced with the new number. |
24 | What happens when you add an entry to a map with a value that already exists in the map? A: Nothing, there are just two values that are the same but have different keys. |
25 | How do you check whether a given key is contained in a map? A: You must use the containsKey() method from the HashMap class. You enter in a key and it returns true if it is contained. Otherwise it'll return false. |
26 | What happens when you try to look up a value, and the key doess not exist in the map? A: It simply returns null if the key doesn't exist, as it has no assigned value. |
27 | How do you check how many entries are contained in a map? A: To check how many entries are contained in a map simply use the size() method from the HashMap class. |
28 | Implement the changes discussed in the text in your own version of the TechSupport system. Test it to get a feel for how well it works. A: I'll complete this later, I don't have the book with me right now, because I thought I didn't need it since I read through the entire chapter already. |
29 | What are the similarities and differences between a HashSet and an ArrayList? HashSet is a set of elements that have no duplicate, like a function in mathematics, while ArrayList is simply a list of elements. |
Thursday, 21 October 2010
Chapter Five
1 | Open and run the project tech-support-complete. You run it by creating an object of class SupportSystem and calling its smart() method. Enter some questions you might be having with your software to try out the system. See how it behaves. Type "bye" when you are done. You do not need to examine the source code at this stage. This project is the complete solution that we will have developed by the end of the chapter. The purpose of this exercise is only to give you an idea of what we plan to achieve. A: |
2 | Investigate the String documentation. Then look at the documentation for some other classes. The Duke University site has documentation for the AP Subset and is very useful for this course because you are not overwhelmed with the full Java library, below. What is the structure of class documentation? Which sections are common to all class descriptions? What is their purpose? A: 1) The structure of class documentation is as follows: First it gives the class signature A description of the class as a whole Then it gives the fields and a summary of each, and how they function A constructors list and summary of each A method list and summary Further field description 2) All the above sections are essentially present in each class descriptions, which makes it simple to follow how each class works when you search for one. 3) The purpose of class documentation is to give the user some guidance on many classes with which he may not have experience, or sections that he hasn't learned fully. For example, I use the string class on a day to day basis, yet I didn't know the string constructors and fields, and especially not ALL the methods. Class documentation is a good method to combat such problems. |
3 | Look up the startsWith() method in the documentation for String. Describe in your own words what it does. A: There are two startsWith() methods, one simply searchs if the String begins with the given prefix, and the other checks if it begins with the given prefix at a given index. That is to say, that in a string "Michael" startsWith("c", 3) would return true. |
4 | Is there a method in the String class that tests whether a string ends with a given suffix? If so, what is it called? and what are its parameters and return type A: Yes, quite logically, this method is named endsWith() and has the parameters of a String named suffix, where suffix is the ending character(s) of the string. |
5 | Is there a method in the String class that returns the number of characters in the string? If so, what is it called? and what are its parameters? A: Yes, it is called length() and takes no parameters. |
6 | If you found methods for the two tasks above, how did you find them? Is it easy or hard to find methods you are looking for? Why? I found them by thinking logically, there only a few words for each method, length() could either be that or size(), and endsWith() was obvious, as there was a method named startsWith(). Documentation is done in a very logically coherent order making it easy for its users. |
7 | Find the trim() method in the String class's documentation. Write down the signature of that method. Write down an example call to that method on a String variable called text. What does the documentation say about the control characters at the begining of the String? A: 1) public String trim() 2) System.out.println(text.trim()) 3) It removes the white space from the beginning and end of the string. |
8 | |
9 | Improve the code of the SupportSystem class in the tech-support1 project so that the case of the input string is ignored. Use the String class's toLowerCase() method to do this. Remember that this method will not actually change the string it is called on, but result in the creation of a new one with slightly different contents. Why is this so? A: Done. Because the string is a field in the InputReader class, and can only be gained through the SupportSystem class which uses the InputReader class, but can't be changed as it is a private field. |
10 | Find the equals() method in the documentation for class String. What is the return type of the method? The return type of the equals() method is boolean. |
11 | Change your implementation to use the equals() method instead of startsWith(). A: Done |
12 | Find the class Random in the Java library documentation. Which package is it in? What does it do? How do you construct an instance? How do you generate a random number? Note that you will probably not understand everything that is stated in the documentation. Just try to find out what you need to know. A: 1) It is in the util package 2) You write for example Random Michael = new Random(101) It's also possible to construct an instance without giving it any parameters. 3) To generate a random numbers you must call the nextInt() method. which will return a number between 0 and the seed field, unless specified with an actual parameter. |
13 | Try to write a small code fragment (on paper) that generates a random number using this class. A: public int random(int numberRange); { Random Michael = new Random(numberRange); return Michael.nextInt(); } |
14 | Write some code to test the generation of random numbers. To do this create a new class called RandomTester. In class RandomTester, implement two methods: printOneRandom() (which prints out one random number) and printMultiRandom(int howMany). A: Done, |
15 | Find the nextInt() method in class Random that allows the target range of random numbers to be specified. What are the possible random numbers that are generated when you call this method with 100 as its parameter? A: Any number between 0 to 99. |
16 | Write a method in your RandomTester class called throwDice() that returns a random number between 1 and 6 (inclusive). A: Done, |
17 | Write a method called getResponse() that randomly returns one of the strings "yes", "no" or "maybe". A: Done, |
18 | Extend your getResponse() method so that it uses an ArrayList to store an arbitary number of responses, and randomly returns one of them. Done. |
Sunday, 17 October 2010
Chapter Four Test
1. One error found in the admin constructor where the while loop forgot to increment i (i++)
However, I could not find the second error, I need assistance finding it. I was considering whether the test is outdated, and the current BlueJ is compatible with one of the terms that were used to mislead us from finding the error, such as using array syntax for ArrayList, as you've said BlueJ can now read.
2. Add a new student to class Admin with 4 courses. Run the class. Does it behave as expected?
2. No, when we use the admin class' method it prints my student's name without his GPA, instead it says 0.0.
3. Implement method setGPA() of class Student. The implementation of this method requires a local variable to sum all the student's grades. This value of this variable will be constructed by looping (for each) through courseList, each time incrementing the value by the grade obtained on the course. The GPA will be determined by dividing the sum of the grades by the number of courses.
Should this method be private or public? Explain.
Should this method be private or public? Explain.
3. public double setGPA()
{
int totalClasses = 0;
int totalGrades = 0;
for(int index = 0; index < courseList.size(); index++)
{
totalClasses++;
totalGrades = totalGrades + courseList.get(index).getGrade();
}
return totalGrades / totalClasses;
}
It should be public, otherwise the method would only be available to the Student class, and the admit class would not be able to reach it.
4. The toString() method is inherited (from class Object) by class Student (all Java classes inherit from the Object class). What is the output when this method is called on a Student object?
Inherited methods can be replaced by methods with the same signatures (as the ancestral method) in the descendent class, thus overriding the inherited method.
Replace the inherited toString() method of class Student, by writing a new method that returns a String containing the student's name and a list of all their course titles.
4. It returned the following string: "Student@1f80cb01".
public String toString()
{
String studentNClass = "";
for(Course element : courseList)
{
studentNClass = studentNClass + name + " : " + element.getCourseTitle() + " ";
}
return studentNClass;
}
5. Create a new field in StudentList named deansList that holds a list of students who are on the Dean's List.
Create a makeDeansList() method that creates the list from all students in StudentList that have GPAs of 88 or higher.
answer (code)
new field..
makeDeansList()
5.
public void makeDeansList()
{
for(Student individual : listOfStudents)
{
if(individual.getGPA() >= 88)
deansList.add(individual);
}
}
6. Create a method named printDeansList() that prints the names of students on the Deans List with their GPAs. Each student should be printed on a new line.
answer (code)
printdeansList()
6. public void printDeansList()
{
for(Student individual : deansList)
{
System.out.println(individual + " With a GPA of: " + individual.getGPA());
}
}
7. Create a method named findTopStudent() that finds the student with the highest GPA and prints..
(example) The top student is Mario with a GPA of 98
answer (code)
findTopStudent()
7. public Student findTopStudent()
{
Student topStudent = deansList.get(0);
for(Student individual : deansList)
{
if(individual.getGPA() > topStudent.getGPA())
topStudent = individual;
}
return topStudent;
}
Wednesday, 13 October 2010
Mike's Awesome ArrayList Practice
I got most of the methods, especially the harder ones.
Here's my source code, hope it's good! :
import java.util.ArrayList;
import java.util.Random;
/**
* Demonstrates simple algorithms using ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
public class ArrayListDemo
{
private ArrayList <Integer> myArrayList;
ArrayListDemo()
{
myArrayList = new ArrayList <Integer>();
}
/**
* postcondition: limit number of values are added to myArrayList. Each value is between 0-(limit-1)
*/
public void fillRandom(int limit)
{
Random r = new Random();
for(int i = 0; i<limit; i++)
myArrayList.add(r.nextInt(limit));
}
/**
* postcondition: writes the ArrayList to console
*/
public void writeArrayList(ArrayList newArrayList)
{
newArrayList = new ArrayList <Integer> ();
}
/**
* postcondition: one int is added to myArrayList
* note that the int value is "autoboxed" or wrapped into an Integer object to be stored in myArrayList
*/
public void addInt(int value)
{
myArrayList.add(value);
}
/**
* postcondition: the value at position index is removed from myArrayList
*/
public void remove(int index)
{
myArrayList.remove(index);
}
/**
* postcondition: returns the index of the first occurrence of target
* returns -1 if target not found
*/
public int sequentialSearch (int target)
{
int i=0;
while(i<myArrayList.size())
{
if(myArrayList.get(i) == target)
return i;
i++;
}
return -1;
}
/**
* returns the minimum value value in myArrayList
*/
public int minvalue ()
{
int min = myArrayList.get(0);
for(int i=0; i<myArrayList.size(); i++)
{
if(myArrayList.get(i)<min)
min = myArrayList.get(i);
}
return min;
}
/**
* returns the sum of the values in myArrayList
*/
public int sum ()
{
int sum = 0;
for(int i=0; i<myArrayList.size(); i++)
{
sum=sum+myArrayList.get(i);
}
return sum;
}
/**
* returns the arithmetic mean
*/
public double mean ()
{
int total = 0;
for(int i=0; i<myArrayList.size(); i++)
{
total = total+myArrayList.get(i);
}
return total/myArrayList.size();
}
/**
* returns an array of Integers using the int values from myArray
*/
public Integer [] myIntegerArray ()
{
Integer [] integerArray;
integerArray = null; // Don't know the how to do this one, see above issue.
return integerArray;
}
/**
* returns a String representation of the numbers in myArrayList
*/
public String numberString ()
{
String str = "";
for(int i=0; i<myArrayList.size(); i++)
{
str = str + myArrayList.get(i);
}
return str;
}
/**
* postcondition: returns true if target found
*
*/
public boolean binarySearch (int target)
{
int i = 0;
while(i<myArrayList.size())
{
if(myArrayList.get(i) == target)
return true;
i++;
}
return false;
}
/**
* sorts myArrayList in ascending order using insertion sort algorithm
* starting at [0] find smallest element, exchange with element at [0]
* starting at [1] find smallest element, exchange with element at [1]
* continue until sorted
*/
public void insertionSort()
{
int min = myArrayList.get(0);
for(int z = 0; z < myArrayList.size(); z++)
{
for(int i = 0; i < myArrayList.size(); i++)
{
if(myArrayList.get(i)<min)
min = myArrayList.get(i);
}
min = myArrayList.set(z, min); //HOLY CRAP I CANNOT BELIEVE I GOT THIS! :)
z++;
}
}
/**
* sorts myArrayList in ascending order using selection sort algorithm
* starting at [1] compare with [0] if not in order exchange
* starting at [2] compare with [1] if not in order exchange and keep moving
* until in correct position
*/
public void selectionSort()
{
int min = myArrayList.get(0);
for(int z = 0; z < myArrayList.size(); z++)
{
for(int i = 1; i < myArrayList.size(); i++)
{
if(myArrayList.get(i)<myArrayList.get(i-1))
myArrayList.set(i-1, myArrayList.get(i));
i++;
}
z++;
} // AND AGAIN!
}
/**
* returns the median value in myArrayList
*/
public int median ()
{
int median = 0;
if(myArrayList.size()/2 == 0)
median = median + (myArrayList.get(myArrayList.size()/2 -1) + myArrayList.get(myArrayList.size()/2+1)/2);
else median = median + myArrayList.get(myArrayList.size()/2 +1);
return median; // I think this is it, but when checking, the median didn't come out correct... I'm not sure of the issue.
}
/**
* returns the mode in myArrayList
*/
public int mode()
{
int mode = 0;
int most = 0;
for(int z = 0; z<myArrayList.size(); z++)
{
for(int i = 0; i<myArrayList.size(); i++)
{
if(myArrayList.get(i) == myArrayList.get(z));
mode = mode + 1;
i++;
}
if(mode > most)
most = mode;
mode = 0;
z++;
}
return most;
}
}
// I feel so much better now that I got these last few! Never thought we'd already use 2 dimensional loops though.
Here's my source code, hope it's good! :
import java.util.ArrayList;
import java.util.Random;
/**
* Demonstrates simple algorithms using ArrayLists
*
* @author (your name)
* @version (a version number or a date)
*/
public class ArrayListDemo
{
private ArrayList <Integer> myArrayList;
ArrayListDemo()
{
myArrayList = new ArrayList <Integer>();
}
/**
* postcondition: limit number of values are added to myArrayList. Each value is between 0-(limit-1)
*/
public void fillRandom(int limit)
{
Random r = new Random();
for(int i = 0; i<limit; i++)
myArrayList.add(r.nextInt(limit));
}
/**
* postcondition: writes the ArrayList to console
*/
public void writeArrayList(ArrayList newArrayList)
{
newArrayList = new ArrayList <Integer> ();
}
/**
* postcondition: one int is added to myArrayList
* note that the int value is "autoboxed" or wrapped into an Integer object to be stored in myArrayList
*/
public void addInt(int value)
{
myArrayList.add(value);
}
/**
* postcondition: the value at position index is removed from myArrayList
*/
public void remove(int index)
{
myArrayList.remove(index);
}
/**
* postcondition: returns the index of the first occurrence of target
* returns -1 if target not found
*/
public int sequentialSearch (int target)
{
int i=0;
while(i<myArrayList.size())
{
if(myArrayList.get(i) == target)
return i;
i++;
}
return -1;
}
/**
* returns the minimum value value in myArrayList
*/
public int minvalue ()
{
int min = myArrayList.get(0);
for(int i=0; i<myArrayList.size(); i++)
{
if(myArrayList.get(i)<min)
min = myArrayList.get(i);
}
return min;
}
/**
* returns the sum of the values in myArrayList
*/
public int sum ()
{
int sum = 0;
for(int i=0; i<myArrayList.size(); i++)
{
sum=sum+myArrayList.get(i);
}
return sum;
}
/**
* returns the arithmetic mean
*/
public double mean ()
{
int total = 0;
for(int i=0; i<myArrayList.size(); i++)
{
total = total+myArrayList.get(i);
}
return total/myArrayList.size();
}
/**
* returns an array of Integers using the int values from myArray
*/
public Integer [] myIntegerArray ()
{
Integer [] integerArray;
integerArray = null; // Don't know the how to do this one, see above issue.
return integerArray;
}
/**
* returns a String representation of the numbers in myArrayList
*/
public String numberString ()
{
String str = "";
for(int i=0; i<myArrayList.size(); i++)
{
str = str + myArrayList.get(i);
}
return str;
}
/**
* postcondition: returns true if target found
*
*/
public boolean binarySearch (int target)
{
int i = 0;
while(i<myArrayList.size())
{
if(myArrayList.get(i) == target)
return true;
i++;
}
return false;
}
/**
* sorts myArrayList in ascending order using insertion sort algorithm
* starting at [0] find smallest element, exchange with element at [0]
* starting at [1] find smallest element, exchange with element at [1]
* continue until sorted
*/
public void insertionSort()
{
int min = myArrayList.get(0);
for(int z = 0; z < myArrayList.size(); z++)
{
for(int i = 0; i < myArrayList.size(); i++)
{
if(myArrayList.get(i)<min)
min = myArrayList.get(i);
}
min = myArrayList.set(z, min); //HOLY CRAP I CANNOT BELIEVE I GOT THIS! :)
z++;
}
}
/**
* sorts myArrayList in ascending order using selection sort algorithm
* starting at [1] compare with [0] if not in order exchange
* starting at [2] compare with [1] if not in order exchange and keep moving
* until in correct position
*/
public void selectionSort()
{
int min = myArrayList.get(0);
for(int z = 0; z < myArrayList.size(); z++)
{
for(int i = 1; i < myArrayList.size(); i++)
{
if(myArrayList.get(i)<myArrayList.get(i-1))
myArrayList.set(i-1, myArrayList.get(i));
i++;
}
z++;
} // AND AGAIN!
}
/**
* returns the median value in myArrayList
*/
public int median ()
{
int median = 0;
if(myArrayList.size()/2 == 0)
median = median + (myArrayList.get(myArrayList.size()/2 -1) + myArrayList.get(myArrayList.size()/2+1)/2);
else median = median + myArrayList.get(myArrayList.size()/2 +1);
return median; // I think this is it, but when checking, the median didn't come out correct... I'm not sure of the issue.
}
/**
* returns the mode in myArrayList
*/
public int mode()
{
int mode = 0;
int most = 0;
for(int z = 0; z<myArrayList.size(); z++)
{
for(int i = 0; i<myArrayList.size(); i++)
{
if(myArrayList.get(i) == myArrayList.get(z));
mode = mode + 1;
i++;
}
if(mode > most)
most = mode;
mode = 0;
z++;
}
return most;
}
}
// I feel so much better now that I got these last few! Never thought we'd already use 2 dimensional loops though.
Subscribe to:
Posts (Atom)