Commit c1101dde authored by Matthew Burket's avatar Matthew Burket

First and last

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_20]"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>project8</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
91183 12.878
91538 11.007
91884 10.351
92164 9.644
92400 8.125
92812 12.629
93155 10.877
93449 10.317
93751 9.903
94194 13.655
\ No newline at end of file
package lab8;
/**
* Class containing some utilities for manipulating arrays.
*/
public class ArrayUtil
{
public static void main(String[] args)
{
// try it out
int[] test = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(toString(test));
reverse(test);
System.out.println(toString(test));
}
/**
* Reverses the contents of the given array.
* @param arr
*/
public static void reverse(int[] arr)
{
// exchange arr[0] with arr[length - 1], arr[1]
// with arr[length - 2], and so on, until we get
// to the middle of the array
int front = 0;
int rear = arr.length - 1;
while (front < rear)
{
// exchange arr[front] with arr[rear] using a temporary variable
int temp = arr[front];
arr[front] = arr[rear];
arr[rear] = temp;
// move indices towards the center
front += 1;
rear -= 1;
}
}
/**
* Returns a string representation of the given array.
* @param arr
* @return
*/
public static String toString(int[] arr)
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; ++i)
{
if (i > 0)
{
// every element after first one has comma before it
sb.append(", ");
}
sb.append(arr[i]);
}
sb.append("]");
return sb.toString();
}
}
\ No newline at end of file
package lab8;
/**
* Class representing a playing card with possible rank
* 1 through 13 and four possible suits.
*/
public class Card
{
/**
* Constants for the four suits.
*/
public enum Suit
{
CLUBS, DIAMONDS, HEARTS, SPADES
};
/**
* Suit for this card.
*/
private final Suit suit;
/**
* Rank for this card.
*/
private final int rank;
/**
* Names used for displaying strings.
*/
private final String[] NAMES =
{
"Dummy", // element 0 not used
"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"
};
/**
* Constructs a card with the given rank and suit. Ranks are
* assumed to be in the range 1 through 13.
* @param givenRank
* rank for this card
* @param givenSuit
* suit for this card
*/
public Card(int givenRank, Suit givenSuit)
{
rank = givenRank;
suit = givenSuit;
}
/**
* Returns the rank for this card.
* @return
* rank for this card
*/
public int getRank()
{
return rank;
}
/**
* Returns the suit for this card.
* @return
* suit for this card
*/
public Suit getSuit()
{
return suit;
}
/**
* Returns a String representation of this card.
*/
public String toString()
{
return NAMES[rank] + " of " + suit;
}
/**
* Returns a String representation of a given array of cards.
* @param arr
* array of Card objects
* @return
* a String representation of the given array
*/
public static String toString(Card[] arr)
{
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; ++i)
{
if (i > 0)
{
// every element after first one has comma before it
sb.append(", ");
}
sb.append(arr[i]);
}
sb.append("]");
return sb.toString();
}
}
\ No newline at end of file
package lab8;
import java.util.ArrayList;
import java.util.Random;
import lab8.Card.Suit;
/**
* Class representing a standard 52-card deck of playing cards from which cards
* can be selected at random.
*/
public class Deck {
/**
* The cards comprising this deck.
*/
private Card[] cards;
/**
* The random number generator to use for selecting cards.
*/
private Random rand;
/**
* Constructs a new deck with a default random number generator.
*/
public Deck() {
rand = new Random();
init();
}
/**
* Constructs a new deck with the given random number generator.
*/
public Deck(Random givenGenerator) {
rand = givenGenerator;
init();
}
/**
* Returns a new array containing k elements selected at random from this
* deck.
*/
public Card[] select(int k) {
// ArrayList<Card> listCards = new ArrayList<Card>();
// for(Card card : cards) {
// listCards.add(card);
// }
// Card[] returnCards = new Card[k];
// for (int i = 0; i < k; i++) {
// int randNum = rand.nextInt(listCards.size());
// returnCards[i] = listCards.get(randNum);
// listCards.remove(randNum);
// }
Card[] returnCards = new Card[k];
for(int i = (k-1); i > 0; i--) {
int randInt = rand.nextInt(i);
Card selectedCard = cards[randInt];
returnCards[i] = selectedCard;
Card endCard = cards[k-i];
cards[k-i] = selectedCard;
cards[randInt] = endCard;
}
returnCards[0] = cards[0];
return returnCards;
}
/**
* Initializes a new deck of 52 cards.
*/
private void init() {
cards = new Card[52];
int index = 0;
for (int rank = 1; rank <= 13; ++rank) {
cards[index] = new Card(rank, Suit.CLUBS);
index += 1;
cards[index] = new Card(rank, Suit.DIAMONDS);
index += 1;
cards[index] = new Card(rank, Suit.HEARTS);
index += 1;
cards[index] = new Card(rank, Suit.SPADES);
index += 1;
}
}
}
\ No newline at end of file
package lab8;
import java.util.Random;
public class DeckTest
{
public static void main(String[] args)
{
Random rand = new Random(42); // "seed" the random number generator with a value we choose
Deck deck = new Deck(rand); // construct the deck to use that generator
Card[] hand = deck.select(52);
System.out.println(Card.toString(hand));
//System.out.println(hand.length);
}
}
\ No newline at end of file
package lab8;
/**
* Data container for recording the odometer reading
* and gallons from filling up the gas tank of a vehicle.
*/
public class FillUp
{
/**
* Odometer reading when the tank was filled.
*/
private final int odometer;
/**
* Gallons needed to fill the tank.
*/
private final double gallons;
/**
* Constructs a new FillUp object with the given data.
* @param givenOdometer
* odometer reading
* @param givenGallons
* number of gallons
*/
public FillUp(int givenOdometer, double givenGallons)
{
odometer = givenOdometer;
gallons = givenGallons;
}
/**
* Returns the odometer reading.
* @return
* the odometer reading
*/
public int getOdometer()
{
return odometer;
}
/**
* Returns the number of gallons.
* @return
* number of gallons
*/
public double getGallons()
{
return gallons;
}
}
\ No newline at end of file
package lab8;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class MileageCalculator {
public static void main(String[] args) throws FileNotFoundException {
ArrayList<FillUp> list = createArrayList();
for(int i = 1; i < list.size(); i++) {
int diff = list.get(i).getOdometer() - list.get(i-1).getOdometer();
System.out.println("Mileage " + i + ": " + diff/list.get(i).getGallons());
}
}
public static ArrayList<FillUp> createArrayList() throws FileNotFoundException {
ArrayList<FillUp> list = new ArrayList<FillUp>();
File file = new File("mileage.txt");
Scanner scan = new Scanner(file);
while(scan.hasNextLine()) {
list.add(new FillUp(scan.nextInt(), scan.nextDouble()));
}
scan.close();
return list;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment