/* * Midterm1InLab.java * Midterm 1 test for CS102, Fall 2008 * * @name Dale Reed // Replace my name with yours * @date Wednesday, October 8th, 2008 * * Be sure to comment out sections of code that don't work before * you turn in your solution, since you get no points if your * program doesn't compile. See instructions for each of the problems * below. */ import java.util.Scanner; // used for console input commands public class Midterm1InLab // For first Midterm { // For input from the keyboard Scanner keyboard = new Scanner(System.in); public static void main (String[] args) { // Replace my name with yours in the statement below System.out.println("Name: Englebert Humberdink \n" + "Midterm Exam 1, CS 102, Fall 2008 \n"); // Create an object so I can call the two methods below Midterm1InLab myMidterm1 = new Midterm1InLab(); // Calling two methods. myMidterm1.problem1(); // first problem myMidterm1.problem2(); // second problem // End of the midterm. System.out.println("\nEnd of Midterm1 problems.\n\n"); }// end main() /* Write the code for your solution to Problem 1 inside the method * called problem1(). * * Write code to read in a String from the keyboard, * then count how many number digits are in the input. * (Hint: Think of the digits as just characters and * remember how we pulled out characters in program #2) * * For one set of input, running this would look like: * Inside Problem 1 * Type in a String: 1 year after I was 12 I turned 13 * How many digits were found: 5 * * For another set of input, running this would look like: * Inside Problem 1 * Type in a String: 7 times 7 is 49 * How many digits were found: 4 */ private void problem1() { System.out.println("Inside Problem 1"); // Write your solution to problem 1 starting here... // get user input System.out.println("Type in a String: "); String userInput = keyboard.nextLine(); int sum = 0; char c; // stores current userInput character // loop through each character until the end of the string, // counting how many digits we find as we go for( int i=0; i< userInput.length(); i++) { c = userInput.charAt( i); // get character at position i // see if it was a digit if( (c>='0') && (c<='9')) { // it IS a digit, so increment sum sum++; // same as sum = sum + 1 } } // display the answer System.out.println("How many digits were found: " + sum); }// end method problem1() /* * Write code to create a Car class. * * In class Car (see below) you must implement the following: * 1. The appropriate 5 class instance variables (fields) * 2. Constructors * 3. Code that allows comparing two cars, * telling whether or not they are equal. * 4. A method that gives properly formatted output when an * instance is displayed using System.out.print or * System.out.println * 5. Any additional methods that you think are needed * * Running this would look like: * Inside Problem 2 * On the next line please enter the name, cost, color, number of doors, and number of cylinders for a car: * Yugo 675 rust 2 4 * * The cars are: * maroon 4-door Toyota costing $21000, 4 cylinder * black 4-door Lexus costing $50000, 6 cylinder * rust 2-door Yugo costing $675, 4 cylinder * * Car2 and car3 are NOT equal. * * End of Midterm1 problems. * * * Running this again could look like: * Inside Problem 2 * On the next line please enter the name, cost, color, number of doors, and number of cylinders for a car: * Lexus 50000 black 4 6 * * The cars are: * maroon 4-door Toyota costing $21000, 4 cylinder * black 4-door Lexus costing $50000, 6 cylinder * black 4-door Lexus costing $50000, 6 cylinder * * Car2 and car3 are equal. * * End of Midterm1 problems. * */ private void problem2() { System.out.println("Inside Problem 2"); // *** IMPORTANT *** // Don't change any code inside of this problem2() method. // Only add code to the definition of class Car at the // bottom of this document. Car car1 = new Car(); Car car2 = new Car("Lexus", 50000, "black", 4, 6); Car car3; System.out.println("On the next line please enter the name, cost, color, number of doors, and number of cylinders for a car: "); String carType = keyboard.next(); int carCost = keyboard.nextInt(); String carColor = keyboard.next(); int carNumberOfDoors = keyboard.nextInt(); int carNumberOfCylinders = keyboard.nextInt(); car3 = new Car( carType, carCost, carColor, carNumberOfDoors, carNumberOfCylinders); // Display the created objects System.out.println(); System.out.println("The cars are: "); System.out.println( car1 ); System.out.println( car2 ); System.out.println( car3 ); System.out.println(); // Compare car2 and car3, // displaying whether or not they are equal if( car2.equals(car3) ) { System.out.println("Car2 and car3 are equal."); } else { System.out.println("Car2 and car3 are NOT equal."); } }// end method problem2() // Car class class Car { // Write your code here to implement the Car class. // It does not need to be in a separate file. // 1. The appropriate 5 class instance variables (fields) // * 2. Constructors // * 3. Code that allows comparing two cars, // * telling whether or not they are equal. // * 4. A method that gives properly formatted output when an // * instance is displayed using System.out.print or // * System.out.println // * 5. Any additional methods that you think are needed // * // * Running this would look like: // * Inside Problem 2 // * On the next line please enter the name, cost, color, number of doors, and number of cylinders for a car: // * Yugo 675 rust 2 4 // Maroon 4-door Toyota costing $21000, 4 cylinder // instance variables (a.k.a. class variables, fields) String type; int price; String color; int doors; int cylinders; // Constructors Car() { type = "Toyota"; price = 21000; color = "Maroon"; doors = 4; cylinders = 4; } Car( String theType, int thePrice, String theColor, int theDoors, int theCylinders) { type = theType; price = thePrice; color = theColor; doors = theDoors; cylinders = theCylinders; } // toString gives output such as: // maroon 4-door Toyota costing $21000, 4 cylinder public String toString() { return( color + " " + doors + "-door " + type + " costing $" + price + ", " + cylinders + " cylinder"); } // equals allows comparing cards public boolean equals( Car otherCar) { return color.equals( otherCar.color) && doors == otherCar.doors && type.equals( otherCar.type) && price == otherCar.price && cylinders == otherCar.cylinders; } }//end class Car }// end of class