Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarer committed Oct 19, 2019
2 parents f298c88 + 6b22a0c commit 41b8059
Show file tree
Hide file tree
Showing 10 changed files with 768 additions and 30 deletions.
2 changes: 1 addition & 1 deletion _sources/Unit1-Getting-Started/topic-1-3-variables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Naming Variables
.. index::
pair: variable; names

While you can name your variable almost anything, there are some rules. A variable name should start with an alphabetic character (like a, b, c, etc.) and can include letters, numbers, and ``-`` or ``_``. It must be all one word with no spaces.
While you can name your variable almost anything, there are some rules. A variable name should start with an alphabetic character (like a, b, c, etc.) and can include letters, numbers, and underscores ``_``. It must be all one word with no spaces.

You can't use any of the keywords or reserved words as variable names in Java (``for``, ``if``, ``class``, ``static``, ``int``, ``double``, etc). For a complete list of keywords and reserved words see http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html.

Expand Down
337 changes: 334 additions & 3 deletions _sources/Unit5-Writing-Classes/Exercises.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,341 @@
.. qnum::
:prefix: 5-12-
:start: 1

Exercises
=========

Coming soon

.. .. mchoice:: qsh_3
.. mchoice:: AP5-1-3
:practice: T

The Liquid class will contain two double attributes for a liquid’s boiling point temperature and freezing point temperature. The class will also contain a constructor.

.. code-block:: java
public class Liquid
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
- .. code-block:: java

private double boilingPoint;
private double freezingPoint;
public Liquid(double boilingPoint, double freezingPoint)
{ /* implementation not shown */ }
+ Correct! The instance variables should be private and the constructor and methods should be public.

- .. code-block:: java

private double boilingPoint;
private double freezingPoint;
private Liquid(double boilingPoint, double freezingPoint)
{ /* implementation not shown */ }
- Constructors should be public.

- .. code-block:: java

private double boilingPoint;
public double freezingPoint;
private Liquid(double freezingPoint, double boilingPoint)
{ /* implementation not shown */ }
- The instance variables should be private and the constructor and methods should be public.

- .. code-block:: java

public double boilingPoint;
public double freezingPoint;
private Liquid(double boilingPoint, double freezingPoint)
{ /* implementation not shown */ }
- The instance variables should be private and the constructor and methods should be public.

- .. code-block:: java

public double freezingPoint;
public double boilingPoint;
public Liquid(double freezingPoint, double boilingPoint)
{ /* implementation not shown */ }
- The instance variables should be private.

.. mchoice:: AP5-1-4
:practice: T
:random:

The Cat class below will contain two String attributes and one int attribute for name, color, and age; a constructor; and an adoptCat method. The adoptCat method is intended to be accessed outside the class.

.. code-block:: java
public class Cat
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
- .. code-block:: java

private String name;
private String color;
private int age;
public Cat()
{ /* implementation not shown */ }
private void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
- Method adoptCat() should be public.

- .. code-block:: java

public String name;
public String color;
public int age;
private Cat()
{ /* implementation not shown */ }
private void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
- Method and constructor should be public, and instance variables should be private.

- .. code-block:: java

private String name;
private String color;
private int age;
public Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
+ Correct! Method and constructor should be public, and instance variables should be private.

- .. code-block:: java

public String name;
public String color;
public int age;
public Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
- Instance variables should be private.

- .. code-block:: java

public String name;
public String color;
public int age;
private Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
- Instance variables should be private, and the constructor should be public.

.. mchoice:: AP5-2-4
:practice: T
:random:

Consider the definition of the Party class below. The class uses the instance variable numOfPeople to indicate how many people are at the party.

.. code-block:: java
public class Party
{
private int numOfPeople;
private String partyHost;
public Party (String name, int people)
{
partyHost = name;
numOfPeople = people;
}
}
Which of the following statements will create a Party object
that represents a party that has three people at it?
- Party p = new Party ("Natasha", "3");

- An int should be passed in as the second parameter, not a String.

- Party p = new Party ("Eduardo", 3);

+ Correct

- Party p = new Party ("Emillio", "three");

- An int should be passed in as the second parameter, not a string.

- Party p = new Party ("Bob", three);

- The word three would be treated as an undeclared variable here.

- Party p = new Party ("Billie", "2+1");

- Strings cannot perform addition.

.. mchoice:: AP5-2-5
:practice: T
:random:
:answer_a: I only
:answer_b: I and II
:answer_c: I and III
:answer_d: I, II, and III
:answer_e: II and III
:correct: d
:feedback_a: II and III can also create a correct Party instance
:feedback_b: III can also create a correct Party instance
:feedback_c: II can also create a correct Party instance
:feedback_d: I, II, and III can successfully create the Party instance.
:feedback_e: I can also create a correct Party instance

Consider the following class definition. Each object of the class Party will store the party host’s name as partyHost, the number of people as numOfPeople, and the capacity that the event can hold as capacity. Which of the following code segments, found in a class other than Party, can be used to create a party hosted by Charlie without anyone there initially, but the place can hold 78 people ?

.. code-block:: java
public class Party
{
private String partyHost;
private int numOfPeople;
private int capacity;
public Party(String name, int num, int cap)
{
partyHost = name;
numOfPeople = num;
capacity = cap;
}
public Party (String name, int cap)
{
partyHost = name;
numOfPeople = 0;
cap = 0;
}
/* Other methods not shown */
}
I. Party b = new Party("Charlie", 78);
II. Party b = new Party("Charlie", 0, 70+8);
III. Party b = new Party("Charlie", 0, 78);
.. mchoice:: AP5-2-6
:practice: T
:random:

Consider the following class definition.

.. code-block:: java
public class Party
{
private int numOfPeople;
private double volumeOfMusic;
/* missing constructor */
}
The following statement appears in a method in a class other than Party. It is intended to create a new Party object p with its attributes set to 10 and 5.0.

.. code-block:: java
Party p = new Party(10, 5.0);
Which of the following can be used to replace /* missing constructor */
so that the object m is correctly created?
- .. code-block:: java

public Party(int first, double second)
{
numOfPeople = 10;
volumeOfMusic = 5.0;
}

- The constructor should be using the local variables to set the instance variables.

- .. code-block:: java

public Party(int first, double second)
{
numOfPeople = first;
volumeOfMusic = second;
}

+ Correct

- .. code-block:: java

public Party(int first, double second)
{
first = 10;
second = 5.0;
}

- The constructor should be using the parameters to set the instance variables.

- .. code-block:: java

public Party(int first, double second)
{
first = numOfPeople;
second = volumeOfMusic;
}

- The constructor should be changing the instance variables, not the local variables.

.. mchoice:: AP5-3-3
:practice: T
:random:

Consider the following class definition that defines a Liquid class with a boilingPoint, a currentTemperature, and a freezingPoint. For example, Liquid water = new Liquid(100, 50, 0); defines a water object with a boiling point of 100, a current temperature of 50, and a freezing temperature of 0.

.. code-block:: java
public class Liquid
{
private int boilingPoint;
private int currentTemp;
private int freezingPoint;
public Liquid(int bp, int ct, int fp)
{
boilingPoint = bp;
currentTemp = ct;
freezingPoint = fp;
}
/* Other methods not shown */
}
Which of the following preconditions is reasonable for the Liquid constructor?

- Precondition: fp \> 0

- Incorrect. The freezing point could be negative.

- Precondition: currentTemp \> 0

- Incorrect. Unable to make this assumption for all liquids.

- Precondition: fp < ct < bp

+ Correct!

- Precondition: fp > ct > bp

- Incorrect. This would again result in freezing point being > boiling point which is impossible.


.. mchoice:: qsh_3
:practice: T
:random:
:answer_a: Lasagna Meow Screeech
:answer_b: Meow Screeech Lasagna
:answer_c: Screeech Meow Lasagna
Expand Down Expand Up @@ -38,7 +369,7 @@ Coming soon
class Garfield extends Cat
{
public Cat()
public Garfield()
{
System.out.print("Lasagna ");
}
Expand Down
Binary file modified _sources/Unit5-Writing-Classes/Figures/PersonClass.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 41b8059

Please sign in to comment.