Organize your code by creating a new directory named lab2 for the java files associated with this lab.
In VSCode, make sure to add your directory to the workspace (at the top level.)
Do not use packages (which VSCode might suggest), as you will not pass the Gradescope autograder tests.
Translate the IceCreamScoop example from Python to Java; include the main function in your IceCreamScoop.java file.
IceCreamScoop.java
Confirm that you can run the program and that you see the following print out:
This vanilla ice cream scoop, topped with ['sprinkles'] has not melted!
This strawberry ice cream scoop, topped with ['whipped cream'] has melted :(
Lists (and ArrayLists)
Arrays
import java.util.List;
import java.util.ArrayList;
public class SampleSequentialData
{
public static void main( String[] args )
{
// demonstrates a (dynamically sized) List, implemented with an ArrayList
List<String> weekendDays = new ArrayList<String>();
weekendDays.add( "Saturday" );
weekendDays.add( "Sunday" );
System.out.println( "Weekends are: " + weekendDays.toString() );
System.out.println( "Weekdays are: ");
// demonstrates a (fixed length) array
String[] weekdays = {"Monday","Tuesday","Wednesday","Thursday","Friday"};
for ( int i = 0; i < weekdays.length; i++ )
{
System.out.print( weekdays[i] + ", " );
}
System.out.println( "" );
weekdays[0] = "Mon";
System.out.println( "And now... weekdays are: ");
for ( int i = 0; i < weekdays.length; i++ )
{
System.out.print( weekdays[i] + ", " );
}
System.out.println( "" );
}
}
import java.util.List;
import java.util.ArrayList;
public class IceCreamScoop
{
/** Represents a single scoop of ice cream. Mmmm. Ice cream. **/
// declare instance properties
private String flavor;
private List<String> toppings;
private boolean melted;
/** Constructor creates a new ice cream scoop
* with the specified flavor. */
public IceCreamScoop( String flav )
{
// initialize an instance property to hold the flavor
flavor = flav;
// initialize the toppings list, which starts off empty
toppings = new ArrayList<String>();
// initialize that the scoop is not melted
melted = false;
}
/** Melt this scoop of ice cream :( **/
public void melt()
{
// update the instance property accordingly
melted = true;
}
/** Add the topping to this scoop's toppings. **/
public void addTopping( String topping )
{
toppings.add( topping );
}
/** Return a string representation of this ice cream scoop. **/
public String toString()
{
String basicDescription = "This " + flavor + " ice cream scoop, topped with "
+ toppings.toString();
if (melted)
return basicDescription + " has melted :(";
else
return basicDescription + " has not melted!";
}
public static void main( String[] args )
{
IceCreamScoop vanillaScoop = new IceCreamScoop( "vanilla" );
vanillaScoop.addTopping( "sprinkles" );
System.out.println( vanillaScoop );
IceCreamScoop strawberryScoop = new IceCreamScoop( "strawberry" );
strawberryScoop.melt();
strawberryScoop.addTopping( "whipped cream" );
System.out.println( strawberryScoop );
}
}
Marker.java
Define a simple class that represents a marker. Write your class definition in a file named Marker.java, adhering to the following specification:
The class should be named Marker.
There should be a two instance properties of type str that maintain:
the color of the marker
the color of the marker's cap
The constructor should take in a single argument for the color of the marker. The cap should be initialized to the same color.
There should be getters for each instance property called getColor and getCapColor.
There should be a setter to change the marker cap color (because this happens in real life!) called setCapColor.
Since markers can’t change color, there will be no setter for the marker's color.
There should be an instance method called toString that takes in no parameters and returns a String representation of the marker. If the marker is red with a blue cap, for example, it should return
"A red marker with a blue cap."
main function
Define the main function so that it:
Instantiates a new Marker instance that represents a red marker and assign it to a variable called redMarker.
Instantiates a new Marker instance that represents a green marker and assign it to a variable called greenMarker.
Prints information about the greenMarker.
Prints information about the redMarker.
Sets the color of the cap of the red marker to be blue.
Prints information about the redMarker.
When you run your program, you should see the following print out:
A green marker with a green cap.
A red marker with a red cap.
A red marker with a blue cap.
Submit the following files to the Gradescope assignment Lab 2: Python to Java.
IceCreamScoop.java
Marker.java