Header image  
Java  
line decor
  HOME :: BUSINESS :: COMPUTING :: LIFESTYLE :: PASTIME ::
line decor
   
 

Java Tutorial in Spanish Invasores



 
 
       

JAVA

mport java.awt.*;
import java.applet.*;

public class hello extends Applet {

public void init() {
}

public void paint(Graphics g) {

g.drawString("Hello Josh", 50, 50);
}
}

Create an HTML file to run it:


<HTML>

<HEAD>

<TITLE> A Simple Program </TITLE>

</HEAD>

<BODY>


Here is the output of my program:

<APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25>

</APPLET>

</BODY>

</HTML>


Lab 4: Collections Framework

This lab consists of four parts. The first three parts are compulsory, while the last part is optional (and will not be graded).

Dense Multiset

A multiset is a set that allows repetition of individual elements. Thus, adding an element always increases the cardinality by one, regardless of whether the element is already present. In this lab, you will implement a multiset of char.

Write a class that implements the MultiSetOfChar interface:

interface MultiSetOfChar {
int getCardinality();
int getElementCount(char target);
Set<Character> getElementSet();
void add(char item);
boolean remove(char target);
char randomUniformChoose();
}
As far as semantics (behavior) of these methods, your implementation should do the following:

getCardinality
Returns the cardinality of the multiset.
getElementCount
Returns the number of occurrences of the element in the multiset. The sum of getElementCount for each char is equal to the cardinality of the set.
getElementSet
Returns a set such that every element in the multiset is in the set (but no duplicates).
add
Adds the argument to the multiset.
remove
Removes the target, if present in the multiset. The method returns true if it changes the multiset.
randomUniformChoose
Returns a char chosen randomly based on the contents of the multiset. This operation does not change the cardinality of the multiset. For a character that appears N times in a multiset of cardinality M, the probability of that character being returned is N / M.
Your implementation should be called DenseMultiSetOfChar and should make use of the Collections Framework. In addition, you should obtimize your implementation for containing many repetitions of relatively few distinct characters. That is, a DenseMultiSetOfChar should not use significantly (any?) more memory if it contains 100,000 repetitions of 'a' vs just one.

For your convenience, the test program TestDenseMultiSetOfCharSimple.java is provided. Your DenseMultiSetOfChar class should compile with this test program. This test program documents the expected behavior so it should help you ensure you have implemented the correct method behaviors as well as signatures.

Compressing a Library of Books

How many characters are in the text of "Alice in Wonderland"? How many times does the character 'A' appear? In this part of the lab, you will implement a class that could be used to keep track of this information for every book in a library.

Write a class that implements the FrequencyLibrary interface:

interface FrequencyLibrary {
int size();
boolean contains(String target);
MultiSetOfChar getFrequencies(String target);
void add(String name, char element);
boolean remove(String name, char element);
char randomUniformChoose(String name);
}
As far as semantics (behavior) of these methods, your implementation should do the following:

size
Returns the number of books in the library.
contains
Returns true if and only if the argument is already a book title in the library.
getFrequencies
Returns the MultiSetOfChar that represents the occurrences of the individual characters in the text of the book indiciated by the argument.
add
Modifies the character occurrences associated with name to include one more occurrence of element.
remove
Modifies the character occurrences associated with name to include one less occurrence of element.
randomUniformChoose
Returns a random character, chosen from the same distribution as the characters appear in the book. For example, if 5% of the characters in "Alice in Wonderland" are an 'A', then this method should return an 'A' about 5% of the time.
Your implementation should be called DenseFrequencyLibrary and should make use of your solution to part 1 as well as the Collections Framework.

For your convenience, the test program TestDenseFrequencyLibrarySimple.java is provided. Your DenseFrequencyLibrary class should compile with this test program. This test program documents the expected behavior so it should help you ensure you have implemented the correct method behaviors as well as signatures.

Performance

Compare the run-time performance of your DenseMultiSetOfChar using two different choices for collection on which it is built. For example, if you wrote DenseMultiSetOfChar to use a HashSet, compare it with using a TreeSet instead. (I am not suggesting or advising you to use a HashSet for your solution! This is just an illustrative example.)

Note: you should be able to change between two implementations (such as HashSet and TreeSet) by changing a single line of code in your implementation.

For your convenience, a basic timing program Profiler.java is provided. Expand and modify this program until you have gathered enough performance data to observe the impact that choice of collection implementation makes on your code. For example, you might want to look at the performance of different methods under different circumstances (e.g., small/large cardinality or few/many repetitions). Include a text file in your submission that describes your findings. (Only a brief discussion is needed: a single paragraph.)

Optional Extra: Generics

Note: This part of the lab is optional. It will not be graded, so you do not have to complete it in order to get full credit for the lab.

Modify the multiset interface provided as part of this lab to use generics. Modify your implementation of this interface accordingly.


import java.util.Set;

interface MultiSetOfChar {
int getCardinality();
int getElementCount(char target);
Set<Character> getElementSet();
void add(char item);
boolean remove(char target);
char randomUniformChoose();
}

=======================

import java.util.Set;

public class TestDenseMultiSetOfCharSimple {

public static void main (String[] args) {
MultiSetOfChar s = new DenseMultiSetOfChar('a');

System.out.println("Cardinality is " + s.getCardinality() + " (should be 1)");
System.out.println("No. of a's is " + s.getElementCount('a') + " (should be 1)");
System.out.println("No. of b's is " + s.getElementCount('b') + " (should be 0)");

Set<Character> basis = s.getElementSet();
System.out.println("Cardinality of basis set is " + basis.size() + " (should be 1)");

s.add('a');
s.add('a');
s.add('b');

System.out.println("Cardinality is " + s.getCardinality() + " (should be 4)");
System.out.println("No. of a's is " + s.getElementCount('a') + " (should be 3)");
System.out.println("No. of b's is " + s.getElementCount('b') + " (should be 1)");

basis = s.getElementSet();
System.out.println("Cardinality of basis set is " + basis.size() + " (should be 2)");

boolean result = s.remove('c');
System.out.println("Able to remove element c? " + result + " (should be false)");

result = s.remove('a');
System.out.println("Able to remove element a? " + result + " (should be true)");

System.out.println("Random string (should have about twice as many a's as b's");
for (int i = 0; i < 20; i++) {
System.out.print(s.randomUniformChoose());
}
System.out.println();
}
}

========================

interface FrequencyLibrary {
int size();
boolean contains(String target);
MultiSetOfChar getFrequencies(String target);
void add(String name, char element);
boolean remove(String name, char element);
char randomUniformChoose(String name);
}

============================

import java.util.Set;

public class TestDenseFrequencyLibrarySimple {

public static void main (String[] args) {
FrequencyLibrary lib = new DenseFrequencyLibrary();

lib.add("Row Your Boat", 'R');
lib.add("Row Your Boat", 'G');
lib.add("Row Your Boat", 'M');
lib.add("Row Your Boat", 'L');

lib.add("Twinkle Twinkle", 'T');
lib.add("Twinkle Twinkle", 'H');
lib.add("Twinkle Twinkle", 'U');
lib.add("Twinkle Twinkle", 'S');

lib.add("If You're Happy", 'I');
lib.add("If You're Happy", 'I');
lib.add("If You're Happy", 'I');
lib.add("If You're Happy", 'I');

lib.add("Cheer", 'O');
lib.add("Cheer", 'H');
lib.add("Cheer", 'I');
lib.add("Cheer", 'O');

System.out.println("Size is " + lib.size() + " (should be 4)");
System.out.println("Contains 'Twinkle Twinkle'? " + lib.contains("Twinkle Twinkle")
+ " (should be true)");
System.out.println("Contains 'Happy Birthday'? " + lib.contains("Happy Birthday")
+ " (should be false)");

MultiSetOfChar freq = lib.getFrequencies("Row Your Boat");

boolean result = lib.remove("Twinkle Twinkle", 'H');
System.out.println("Able to remove element 'H' from 'Twinkle Twinkle'? " + result
+ " (should be true)");

result = lib.remove("Happy Birthday", 'H');
System.out.println("Able to remove element 'H' from 'Happy Birthday'? " + result
+ " (should be false)");

System.out.println("Random string (should have approx same number of R/G/M/L):");
for (int i = 0; i < 40; i++) {
System.out.print(lib.randomUniformChoose("Row Your Boat"));
}
System.out.println();

System.out.println("Random string (all should be I):");
for (int i = 0; i < 40; i++) {
System.out.print(lib.randomUniformChoose("If You're Happy"));
}
System.out.println();

System.out.println("Random string (*half* should be O):");
for (int i = 0; i < 40; i++) {
System.out.print(lib.randomUniformChoose("Cheer"));
}
System.out.println();
}
}

=============================

public class Profiler {

private static void insertMany(int count) {
MultiSetOfChar s = new DenseMultiSetOfChar();
for (int i = 0; i < count; i++) {
s.add('a');
}
}

public static void main (String[] args) {

{
long sum = 0;
final long numTrials = 10;
final int numOps = 100000;
System.out.print("Timing " + numOps + " additions with "
+ numTrials + " repetitions... ");
for (int i = 0; i < numTrials; i++) {
long start = System.currentTimeMillis();
insertMany(numOps);
long stop = System.currentTimeMillis();
sum += stop - start;
}
double avg = sum / (double) numTrials;
System.out.println("avg time: " + avg + "ms");
}

}

}

==============================