
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Sample answers and java code for prelim 3 of the cs101 course offered in fall 2006. It includes solutions for various programming problems, such as finding the length of a subarray with identical elements and sorting deals based on price and condition.
Typology: Exams
1 / 1
This page cannot be seen from the preview
Don't miss anything!

1. x= 0;
2. /** = index of the seller in deal[h..k] with best deal.
Precondition: d[h..k] is not empty*/
private int findBest( int h, int k) {
int best= h;
// inv: d[best] is the best deal in d[h..j-1]
for ( int j= h+1; j <= k; j= j+1) {
Deal n= deal.get(j);
Deal b= deal.get(h);
if (n.getPrice() < b.getPrice() ||
(n.getPrice() == b.getPrice() &&
n.getCondition() > b.getCondition())) {
best= j;
return best;
/** Sort the deals for the textbook by price … */
public void sortSellers() {
// inv: deal[0..k-1] is sorted, and
// elements in deal[0..k-1] are better than
// elements in d[k..deal.size()-1]
for ( int k= 0; k != deal.size(); k= k+1) {
int j= findBest(k,deal.size()-1);
Deal temp= deal.get(j);
deal.set(j, deal.get(k));
deal.set(k, temp);
(a) int [][] b= new int [][] { {1, 3, 6, 10}, {2, 5, 9, 13},
(b)
(c) c[2].length
c a
null
a
a
a
a
a
box
GUI( int )
addButtons( int , int )
null
a
JFrame
n cp 2
GUI: 1 a
null