Creating Classes in Java: A Step-by-Step Guide with Acme Anvil Example, Assignments of Biology

’ établissement de la cohérence entre vue fonctionnelle et vue structurelle s ’ établit par l ’ intermédiairede la vue comportementale : vue fonctionnelle cohérente avec vue comportementale (Diagramme deSéquence DS) : 1 Use Case représenté par au moins 1 DS ; chaque DS doit être cohérent avec ce quiest défini dans la vue structurell

Typology: Assignments

2020/2021

Uploaded on 03/20/2021

samer-ben-mim-1
samer-ben-mim-1 🇹🇳

6 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 3
Creating Classes
Scenario
Congratulations! You have just been hired by the Acme Company, a worldwide
conglomerate with a diverse product portfolio, from adding machines to X-ray
machines (see http://en.wikipedia.org/wiki/Acme_Corporation). One of its best-
known products is the ever-popular Acme anvil.
Over the remaining labs, as a member of Acme, you are going to help Acme build a
product order system.
Particularly, in this lab you make your first Java class for this project. A class defines
a type that is a template for objects. It is your job to create a utility class called
MyDate. An application that will use and test your version of the MyDate type has
already been created. The test code appears below.
public class TestMyDate{
public static void main(String[] args){
MyDate date1 = new MyDate(11,11,1918);
MyDate date2 = new MyDate();
date2.day = 11;
date2.month = 11;
date2.year = 1918;
MyDate date3 = new MyDate();
date3.setDate(4,21,1968);
String str1 = date1.toString();
String str2 = date2.toString();
String str3 = date3.toString();
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
Step 1: Create a MyDate class
In this step, you create a MyDate type. The Date type already exists in Java (see
java.util.Date and java.util.Calendar). However, you create another much simpler
date type called MyDate to demonstrate object-orientated programming (OOP)
pf3
pf4
pf5

Partial preview of the text

Download Creating Classes in Java: A Step-by-Step Guide with Acme Anvil Example and more Assignments Biology in PDF only on Docsity!

Scenario

Congratulations! You have just been hired by the Acme Company, a worldwide

conglomerate with a diverse product portfolio, from adding machines to X-ray

machines (see http://en.wikipedia.org/wiki/Acme_Corporation). One of its best-

known products is the ever-popular Acme anvil.

Over the remaining labs, as a member of Acme, you are going to help Acme build a

product order system.

Particularly, in this lab you make your first Java class for this project. A class defines

a type that is a template for objects. It is your job to create a utility class called

MyDate. An application that will use and test your version of the MyDate type has

already been created. The test code appears below.

public class TestMyDate{ public static void main(String[] args){ MyDate date1 = new MyDate(11,11,1918); MyDate date2 = new MyDate(); date2.day = 11; date2.month = 11; date2.year = 1918; MyDate date3 = new MyDate(); date3.setDate(4,21,1968); String str1 = date1.toString(); String str2 = date2.toString(); String str3 = date3.toString(); System.out.println(str1); System.out.println(str2); System.out.println(str3); } }

Step 1: Create a MyDate class

In this step, you create a MyDate type. The Date type already exists in Java (see

java.util.Date and java.util.Calendar). However, you create another much simpler

date type called MyDate to demonstrate object-orientated programming (OOP)

concepts. You will modify MyDate later to demonstrate more advanced OOP

concepts.

1.1 Make a “Java Project” to store your new classes and code.

1.1.1 Select File > New > Java Project ... 1.1.2 In the window that appears, enter AcmeOrderSystem as the project name and click the Finish button.

1.3 Add day, month, and year attributes to MyDate. These three attributes

(member variables) should be of type int.

int day; int year; int month;

Step 2: Create two constructors

Constructors allow you to initialize an object when it is created (instantiated). You

are required to add two constructors to the MyDate class.

2.1 Add a no-argument (no-args) constructor. The no-args constructor enables

you to make a MyDate using default values.

public MyDate(){}

2.2 Add a constructor that has three integer arguments (parameters). This will

enable the user to create a MyDate using a constructor like this: new

MyDate(2, 6, 2004)

public MyDate(int m, int d, int y){ //...use the parameters of m, d and y to set the three attributes }

 Note: As a reminder, constructors look like a method with no return type.

2.3 Save your file, and fix any compiler errors before moving to the next step.

Step 3: Add some methods to MyDate

The MyDate class you have created needs two methods. Recall that a method

represents an action or something an object can do. Users of MyDate should be able

to see the dates represented by the object. Therefore, you must provide a method,

called toString( ), that turns the MyDate object into a String. Users of MyDate should

also be able to reset the date represented by MyDate by feeding in three parameters,

so you must create a setDate( ) method.

3.1 In the MyDate.java editor, add a toString( ) method. The code below shows

a partially completed method. The method should return a String that

contains the values of day, month, and year.

public String toString(){ //TODO return a string with month/day/year like “01/20/1964” return ""; }

 Note : Java Strings can be concatenated using the + symbol like “Cat” + “Dog”, so the

month and day can be concatenated as month + “/” + day.

3.2 Create a setDate(m, d, y) method. The setDate( ) method enables the user

to call one method to set day, month, and year of a MyDate object. An

empty version of this method appears below.

public void setDate(int m, int d, int y){ //TODO set the MyDate attributes with m, d, and y values here! }

Step 4: Run TestMyDate

4.1 A program is provided to test your new class. It appeared at the beginning of

this lab. Test it.

4.2 Fix any errors in MyDate or the TestMyDate file. The most likely place for an

error is in the MyDate type. If you didn’t follow the directions exactly, the test

program can fail to compile.

Bonus Lab Creating Classes

Step 5: Add an initialization block

Add an initialization block to MyDate that defaults the day, month, and year to

January 1, 2000. After completing the initialization block, add code similar to that

shown below to test your initialization block in the main method of

TestMyDate.java.

MyDate date4 = new MyDate(); String str4 = date4.toString(); System. out .println(str4);