remedial tutorials week 2 how to think about programming
Post on 19-Dec-2015
232 views
TRANSCRIPT
The Problem
• Write a "Contact List" application that can:1. store contacts (with a first name, last name, and year of birth)2. get the age in years of the contact3. get the full name (first + last) of the contact4. add contacts to the list5. get the number of contacts in the list
• And a few other things:6. can find the oldest contact in the list7. can find the contact with the longest name in the list8. can ask how many contacts to input, and then allow the user to
input the data for those contacts9. can print out the count of contacts in the list10.can print out the name of the longest contact11.can print out the name of the oldest contact.
What is the first thing to do?
ASK THE HIGHLY... 1 CREATE A CLASS... 1
BREAK IT DOWN I... 1 CREATE A CLASS.... 1
BULID A CLASS S... 1 CREATE A CONTA... 1
CLASS CONTACT... 1 CREATE A WAY T... 1
CREATE A CLASS... 1 OTHER 9
The Process
• Identify the requirements• Brainstorm/design/discuss/redesign the
structure of the program– How many classes do we need?– What are they responsible for?
• Define the classes (constructors, fields and signatures of functions in them)
• Write the code for the functions– Actually lots of steps
• Test and debug!
How many classes do we need?(Think which ones and then respond)
18%
53%
12% 12%
0%0%
6%
1. One
2. Two
3. Three
4. Four
5. Five
6. Six
7. More than 6
The Classes1. ContactList
– Holds all the contacts– Adds and returns contacts etc.
2. Contact– Holds details for each contact– Sets and returns details for each contact
3. Driver– Holds the “main” class – Interacts with the user (reads from
keyboard, prints on screen)– Interacts with the other classes
The Contact class
• What information does it need to store?
• Look at the problem sheet and decide (in pairs) what data fields are needed and which types they should be.
Getters and Setters
• public void setFirstName(String firstName)
• public String getFirstName()
• public void setLastName(String lastName)
• public String getLastName()
• public void setYearOfBirth(int yearOfBirth)
• public int getYearOfBirth()
What else does this class need to be complete?
:S 1 CONSTRUCTOR 1
A CLOSING BRAC... 1 CONTACT AGE, F... 1
ASK THEIR DATE... 1 CONTRUCTORS I... 1
CALCULATEAGE(I... 1 GET AGE OF CON... 1
CATCH ERROR E... 1 OTHER 5
The Constructor
public Contact(String firstName, String lastName, int yearOfBirth) {
this.firstName = firstName;this.lastName = lastName;this.yearOfBirth = yearOfBirth;
}
Other Functions
• public String getFullName()– Should return the first and last names
combined
• public int getAge()– Should return the current age, in years, of the
contact
The Contact class so far…public class Contact {
private String firstName;private String lastName;private int yearOfBirth;
public Contact(String firstName, String lastName, int yearOfBirth) {
this.firstName = firstName;this.lastName = lastName;this.yearOfBirth = yearOfBirth;
}
public String getLastName() {
}
public void setLastName(String lastName) {}
public int getYearOfBirth() {}
public void setYearOfBirth(int yearOfBirth) {}
public void setFirstName(String firstName) {}
public int getAge() {}
public String getFullName() {}
}
Getters and Setterspublic String getLastName() {
return this.lastName;}
public void setLastName(String lastName) {this.lastName = lastName;
}
public int getYearOfBirth() {return this.yearOfBirth;
}
public void setYearOfBirth(int yearOfBirth) {this.yearOfBirth = yearOfBirth;
}
public void setFirstName(String firstName) {this.firstName = firstName;
}
public String getFirstName() {return this.firstName;
}
The ContactList class
• What should go in it?
• Look at the problem sheet and decide (in pairs) what data it needs.
• What functions does it need?
How do well do you understand how to design classes?
1. What is a class?
2. I think I could do it
3. I am pretty sure I could design a class from a specification
4. Class are my friends
ResponseWare enabled me to receive effective feedback in this class.
23%
0%
46%
31%
0%
1. Strongly Disagree
2. Disagree
3. Neutral
4. Agree
5. Strongly Agree
ResponseWare enabled me to influence the direction of the class
1 2 3 4 5
8%
23%
8%
31%31%1. Strongly Disagree
2. Disagree
3. Neutral
4. Agree
5. Strongly Agree
Before the class: How did you feel about the material covered today
Notconfident at
all
Mostly not
confident
Slightly
confident
Neutral
Somewhat
confident
Mostly
confident
Absolutely
confident
0%
21%
14%
7%
14%
29%
14%
1. Not confident at all
2. Mostly not confident
3. Slightly confident
4. Neutral
5. Somewhat confident
6. Mostly confident
7. Absolutely confident
How do you feel now about the material covered today?
1 2 3 4 5 6 7
0% 0% 0%
15%
31%
54%
0%
1. Not confident at all
2. Mostly not confident
3. Slightly confident
4. Neutral
5. Somewhat confident
6. Mostly confident
7. Absolutely confident
Please let us know two things you will take away from class today
They can be as long or as short as you like and they won’t display on this slide. We also won’t know who has said what!
Using ResponseWare for parts of standard labs would be useful
0% 0% 0%
80%
20%
1. Strongly Disagree
2. Disagree
3. Neutral
4. Agree
5. Strongly Agree