Swapping two integer (or int) variables is a task that is taught in most

advertisement
First Semester Programming Course Selection
Basic Course Information/Expected Background
15-100: taken by students with no or little programming experience.
15-200: taken by students with a solid programming experience in any language (if not Java, learn it over the
summer); comfortable programming with Java’s primitive data types, declarations, operators, expressions,
control structures (sequence, decision, loop), methods (arguments, parameters, result), arrays, and using/writing
classes (constructors, methods, fields); comfortable with designing, writing, and debugging programs (e.g.,
scored 3-5 on the AP/CS A exam, or 2-4 on the AP/CS AB exam). This material is covered in 15-100.
15-123: taken by students with a lot of Java programming, who understand the material above, as well as
exceptions (throwing, catching, defining), interfaces, inheritance, using/implementing collection classes (e.g.,
the Map, Set, and List interfaces) and iterators, reading Javadoc, linked lists (header, trailer, circular), trees
(binary search trees, heaps, expression trees), graphs, and recursion (structural, backtracking); familiar with
big-O notation for analyzing algorithms (e.g., scored 5 on the AP/CS AB exam; note this course follows 15200, although it is numbered lower). This material is covered in 15-200.
Please describe your programming background; if any (list only the most important/relevant information)
Check the one box that best describes your attitude about your first programming course at CMU.
 I want to ensure that I have mastered the fundamentals, possibly reviewing material I am familiar with.
 I want to be challenged to learn as many new things as possible, catching up on my own if necessary.
Feel free to specify a more nuanced attitude in the box below
Reflecting on the information above, which course do you think is the best choice for you?
Note: 15-100 and 15-200 lectures are scheduled to meet on the same days, at the same time, so it is easy to
transfer between these courses during the first few weeks, if you discover that you are incorrectly placed.
First Best Choice
Second Best Choice
15-100


15-200


15-123


Beyond 15-123


If you are interested in starting beyond 15-100, please write the class specified on the next page. Do not
worry about syntactic mistakes. The purpose of this exercise is for you to demonstrate (from your long term
memory) your general knowledge of programming. Doing so will help us to place you in the right programming
course. Therefore, do not look-up material in a book or on the web; do not talk to anyone; and, do not test/debug
your code on a computer. You should write this class in one sitting, indicating at the bottom of the next page the
approximate amount of time that you spent reading/understanding the description and writing your code.
Write a class named Frequency that uses an array (not a collection class) to store how frequently each int
score occurs in a sequence of tallied test scores. The code below uses this class by prompting the user for the
scores: notice the constructor, and the methods tally, getMaxScore, getTally, and getErrorCount.
Frequency f = new Frequency(100); //Track scores in the range 0-100, inclusive
for (;;) {
int score = Prompt.forInt("Enter score (-1 to quit)");
if (score == -1)
break;
f.tally(score);
//Increase the frequency of the entered score, by 1
}
for (int i=0; i<=f.getMaxScore(); i++)//0 to biggest score, print it and its tallied value
System.out.println(i + ": frequency = " + f.getTally(i));
System.out.println("Tallied" + f.getErrorCount() + "out-of-bounds scores");
The tally method increases the frequency of occurrence of the score specified by its int parameter. If the score is
not in the correct range, it should increment an error count, whose value is returned by getErrorCount. The
getTally method returns the frequency of the specified score. The getMaxScore method returns the biggest
score whose frequency is >0 (or -1, if there are no scores tallied). Explain any other assumptions that you
make.
Write simple and straightforward code. Do not store redundant instance variables or write redundant code (even
if you would do so for stylistic reasons; write helper methods if useful): avoid redundancy in data and code.
Time Spent:
mins
public class Frequency {
private int[] frequency;
private int errorCount = 0; //Could omit = 0, but included for pragmatics
public Frequency (int max)
{frequency = new int[max+1];} //includes 0 through max inclusive; exception if <-1
private boolean isLegalScore (int score)
{return score >= 0 && score <= frequency.length-1;}
public void tally(int i)
{
if (isLegalScore(i))
frequency[i]++;
else
errorCount++;
}
public int getMaxScore()
{
for (int i=frequency.length-1; i>0; i--)
if (frequency[i] != 0)
return i;
return -1;
}
public int getTally(int score)
{return (isLegalScore(score) ? frequency[score] : 0);}
public int getErrorCount()
{return errorCount;}
}
Download