Board info:
  • No birthdays today
  • It is currently Wed Dec 13, 2017 5:00 pm

1710 Java Assignment 3 - Error Check, Conversion, Card

Ask your questions or share your knowledge on Java Platform SE 6 Programming

1710 Java Assignment 3 - Error Check, Conversion, Card

Postby ali » Thu Apr 05, 2012 4:09 pm

Question 1 (20 marks) Textbook: Page 217 Review Exercise R5.1

Find the errors in the following if statements.

a)
Code: Select all
if quarters > 0 then System.out.println(quarters + " quarters");


b)
Code: Select all
if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x;


c)
Code: Select all
if (x = 1) y++; else if (x = 2) y = y + 2;


d)
Code: Select all
if (x && y == 0) { x = 1; y = 1; }


e)
Code: Select all
if (1 <= x <= 10)
           System.out.println(x);


f)
Code: Select all
if (!s.equals("nickels") || !s.equals("pennies")
              || !s.equals("dimes") || !s.equals("quarters"))
           System.out.print("Input error!");


g)
Code: Select all
if (input.equalsIgnoreCase("N") || "NO")
           return;


h)
Code: Select all
int x = Integer.parseInt(input);
        if (x != null) y = y + x;


i)
Code: Select all
language = "English";
        if (country.equals("US"))
           if (state.equals("PR")) language = "Spanish";
        else if (country.equals("China")
           language = "Chinese";


Question 2 (40 marks) Textbook: Page 221 Programming Exercise P5.2

Write a program that takes user input describing a playing card in the following shorthand notation:
Notation Meaning
A Ace
2...10 Card values
J Jack
Q Queen
K King
D Diamonds
H Hearts
S Spades
C Clubs

Your program should print the full description of the card. For example,

Enter the card notation:
4S
Four of spades


Implement a class Card whose constructor takes the card notation string and whose getDescription method returns a description of the card. If the notation string is not in the correct format, the getDescription method should return the string "Unknown".

Question 3 (40 marks) Textbook: Page 223 Programming Exercise P5.14

Write a unit conversion program that asks users to identify the unit from which they want to convert and the unit to which they want to convert. Legal units are in, ft, mi, mm, cm, m, and km. Define two objects of a class UnitConverter that convert between meters and a given unit.

Convert from:
in
Convert to:
mm
Value:
10
10 in = 254 mm



(The solutions are in the reply post below.)
ali
 
Posts: 25
Joined: Fri Jul 15, 2011 7:43 pm

Re: 1710 Java Assignment 3 - Error Check, Conversion, Card

Postby ali » Thu Apr 05, 2012 4:19 pm

Question 1 Solution:

Part a:
Code: Select all
if quarters > 0 then System.out.println(quarters + " quarters");


Missing parentheses (quarters > 0) and illegal keyword then

Part b:
Code: Select all
if (1 + x > Math.pow(x, Math.sqrt(2)) y = y + x;


Unbalanced parentheses around condition, use:

Code: Select all
if (1 + x > Math.pow(x, Math.sqrt(2))) y = y + x;


Part c:
Code: Select all
if (x = 1) y++; else if (x = 2) y = y + 2;


(x = 1) and (x = 2) are assignments, not comparisons, use (x == 1) and (x == 2) instead.

Part d:
Code: Select all
if (x && y == 0) { x =1; y =1; }


Incompatible variable types: If x, y are integer variables, logical operations like && cannot be performed on them. If x, y are Boolean variables, they can't be compared to 0. Use the following instead:

if (x == 0 && y == 0) . . .;

Part e:

Code: Select all
if (1 <= x <= 10)
   System.out.println(x);


Invalid condition, use if(1 <= x && x <= 10) instead.

Part f:

Code: Select all
if (!s.equals("nickels") || !s.equals("pennies")
   || !s.equals("dimes") || !s.equals("quarters"))
   System.out.print("Input error!");


The condition is always true. If s must be one of the choices, use the following instead:

Code: Select all
if (!s.equals("nickels") && !s.equals("pennies")
    && !s.equals("dimes") && !s.equals("quarters"))
   System.out.print("Input error!");


Part g:

Code: Select all
if (input.equalsIgnoreCase("N") || "NO")
   return;


Invalid compound condition, use the following instead:

if (input.equalsIgnoreCase("N") || input.equalsIgnoreCase("NO"))
. . .

Part h:

Code: Select all
int x = Integer.parseInt(input);
if (x != null) y = y + x;


The integer value x can never be null. Use

Code: Select all
if (input != null) y = y + Integer.parseInt(input);


Part i:

Code: Select all
language = "English";
if (country.equals("US"))
   if (state.equals("PR")) language = "Spanish";
else if (country.equals("China"))
   language = "Chinese";


The indentation levels give the wrong impression: the else actually pairs with the closest if. Use braces:

Code: Select all
language = "English"
if (country.equals("US"))
{
   if (state.equals("PR")) language = "Spanish";
}
else if (country.equals("China"))
   language = "Chinese";


Question 2 Solution:

Card.java
Code: Select all
/**
   This class implements the full description of a deck of cards.
*/
public class Card
{
   /**
      Constructs a card type and suit of a deck of cards.
      @param notation the string describing the card
   */
   public Card(String notation)
   {
      int n = notation.length();
      if (n < 2)
      {
         type = "?";
         suit = "?";
      }
      else
      {
         suit = notation.substring(n - 1, n);
         type = notation.substring(0, n - 1);
      }
   }

   /**
      Get the description of the type and suit of the card.
      @return the description of the the card
   */
   public String getDescription()
   {
      String output = "";

      // Get the type description
      if (type.equalsIgnoreCase("A"))
         output = "Ace";
      else if(type.equalsIgnoreCase("K"))
         output = "King";
      else if(type.equalsIgnoreCase("Q"))
         output = "Queen";
      else if(type.equalsIgnoreCase("J"))
         output = "Jack";
      else if(type.equals("2"))
         output = "Two";
      else if(type.equals("3"))
         output = "Three";
      else if(type.equals("4"))
         output = "Four";
      else if(type.equals("5"))
         output = "Five";
      else if(type.equals("6"))
         output = "Six";
      else if(type.equals("7"))
         output = "Seven";
      else if(type.equals("8"))
         output = "Eight";
      else if(type.equals("9"))
         output = "Nine";
      else if(type.equals("10"))
         output = "Ten";
      else
         return "Unknown";
      output = output + " of ";
      // Get the suit description
      if (suit.equalsIgnoreCase("S"))
         output = output + "Spades";
      else if(suit.equalsIgnoreCase("H"))
         output = output + "Hearts";
      else if(suit.equalsIgnoreCase("D"))
         output = output + "Diamonds";
      else if(suit.equalsIgnoreCase("C"))
         output = output + "Clubs";
      else
         return "Unknown";

      return output;
   }

   private String type;
   private String suit;
}


CardTester.java
Code: Select all
public class CardTester
{
   public static void main(String[] args)
   {     
      System.out.println(new Card("4S").getDescription());
      System.out.println("Expected: Four of Spades"); 
      System.out.println(new Card("QC").getDescription());
      System.out.println("Expected: Queen of Clubs"); 
      System.out.println(new Card("AD").getDescription());
      System.out.println("Expected: Ace of Diamonds"); 
      System.out.println(new Card("10S").getDescription());
      System.out.println("Expected: Ten of Spades"); 
      System.out.println(new Card("CA").getDescription());
      System.out.println("Expected: Unknown"); 
      System.out.println(new Card("").getDescription());
      System.out.println("Expected: Unknown"); 
      System.out.println(new Card("?").getDescription());
      System.out.println("Expected: Unknown"); 
   }
}


Question 3 Solution:

UnitConverter.java
Code: Select all
/**
   This class implements a unit converter that converts between a given
   length unit and meters.
*/
public class UnitConverter
{
   /**
      Constructs a unit converter to convert between a unit and meters
      @param unit the unit used by this converter
   */
   public UnitConverter(String unit)
   {
      final double INCH_TO_METER = 0.0254;
      final double FOOT_TO_METER = 0.305;
      final double MILE_TO_METER = 1609;

      if (unit.equals("in"))
         factor = INCH_TO_METER;
      else if (unit.equals("ft"))
         factor = FOOT_TO_METER;
      else if (unit.equals("mi"))
         factor = MILE_TO_METER;
      else if (unit.equals("mm"))
         factor = 0.001;
      else if (unit.equals("cm"))
         factor = 0.01;
      else if (unit.equals("m"))
         factor = 1;
      else if (unit.equals("km"))
         factor = 1000;
      else
         factor = 1;
   }

   /**
      Converts a measurement to meters.
      @param measurement a measurement in the units of this converter     
      @return the equivalent meters
   */
   public double toMeters(double measurement)
   {
      return measurement * factor;
   }

   /**
      Converts a measurement from meters.
      @param measurement the measurement to convert from meters
      @return the equivalent value in the unit of this converter
   */
   public double fromMeters(double measurement)
   {
      return measurement / factor;
   }

   private double factor;
}
ali
 
Posts: 25
Joined: Fri Jul 15, 2011 7:43 pm


Return to Java SE 6 Programming

cron