Project with classes:
|
Returns:
|

|
Approach:
Approaching a problem that will involve many classes - we need some
kind of strategy, a plan or set of steps that will make a good basis
for these kinds of problems.
Here is one set of steps that seems like it works for many such
problems.
- List classes
- From specifications.
- Developing a good set of classes corresponding to general
problem statement is often very subtle and is really something
of an art.
- There typically is not only one good solution, and
there is often not a BEST solution.
- Class hierarchy
- Decide on parent and child classes.
- You many need to change your mind about this as a project
goes along, so don't think this is set in concrete.
- Attributes
- Which attributes should be where in the class hierarchy?
- Here you should consider moving attributes around in the
class hierarchy.
- For example, if an attribute is common to ALL the children
of a class, it would be a good idea to move that attribute to
the parent class.
- Consider using classes already defined in the JDK. For
example, when working with GUI's, extending JButton can be a
nice solution.
- Types
- Give attributes types (int, double, String, some other
class)
- Constructors
- Parameter lists
- Default no-parameter constructor
- Automatic
- BUT - will be hidden if there are any explicit
constructors
- Hidden ones may cause problems in hierarchy since a child
constructor may implicitly call the parent no-parameter
constructor
- Consider Scanner constructors
- Consider super
and this
calls
- Methods
- public
String toString () method in pretty much ALL
classes
- use toString
methods of parent classes as appropriate
- parameter lists
- consider parent versions with and over one in this class
- overriding and overloading
- Output
- Get output working
- We want to see results as soon as possible
- Data files?
- If the problem is larger, we want to work with data files
quickly to check everything out
- Format:
- Decide on data file format -
- At least at first we want these to be as simple as
possible
- Should include creating objects of each class
- Suggest one line in the file for each class
- Ignore lines that don't define one of the classes - this
allows comments and convenient spacing in the file
- Since Scanner works with space delimiters, try to use
space delimiters in the file
- Reading:
- Scanner to read the file line by line (hasNextLine,
nextLine)
- Instantiate a new Scanner on that line
- Use that Scanner to process the line
- Create a new instance of the class defined on that line
(switch is a good idea here)
- Pass the rest of the line Scanner to a constructor for
that class
- In the constructor, pass the line Scanner reference to
parent Scanner constructors as appropriate
- Use hasNext [int, double] and next [Int, Double] as
appropriate
- May use exception handling - this can get rather complex
and each get next type will have somewhat different
exceptions
- Printing:
- System.out.println
("Read >" + v + "<");
// cool way to check that line was read, and the
constructors and toString methods are in harmony
Class Code Structure
So here is an outline of many of the common elements of a class.
- Stuff in <>'s may be filled in either with a name or
some (a lot) of code
- Stuff in []'s is optional.
- The order of elements inside a class does not matter to the
Java compiler, but following conventions makes is a lot easier
to write and read the code.
[public] class
<name> // public: name = file_name.java, source code
file may have other classes
[extends <some class>] // default is Object
[implements <some inteface(s)>] // we will talk more
about later in this course
{ // start class body
<static variables>
<instance variables>
<constructors>
<static methods>
<instance methods>
public String toString () {} // a REALLY
GOOD IDEA in pretty much all classes
[public static void main (String args []) {}]
// may be a good idea in many classes
} // end class
Coding Sequence
- Create a parent class, something like:
public class
MyTestClass {
} // end class MyTestClass
- Note that this is kind of trivial, but if you have errors
here, there is no reason to go on writing code
- Add a main method.
- Add a simple output line, something like the following, and
run it.
System.out.println
("Bye");
- Add class declarations per design and requirements (see above
for some thoughts about appropriate classes)
Something like the following, just the barest outline:
class MyClassA
{}
class MyClassB extends MyClass A {}
- This should compile, even if this does pretty nothing else
- Get some output:
- in each class - one at a time:
- create a toString method in a class
- in main
- instantiate the class
MyClassA x
= new MyClassA ();
- print out x using MyClassA toString method:
System.out.println
("x: >" + x + "<");
- Declare appropriate attributes
- Types - each attribute should have a type, this is the time
to nail those down
- Names - each attribute should have an appropriate name
- public vs private?
- if this is going to be a big project, it is a good idea to
declare all the variables private and use getters and
setters to control access to the variables.
- if you need security, it is a really good idea to
implement right off the bat
- Initialize attributes
- null is ok for class references
- Default initial values
- for class static and instance variables is 0,
- but that can get confusing
- Suggestion: give each attribute some "junk" value, so if
this value prints or shows up, you will know that some
expected update to the value didn't happen
- Read values
- from console
- from a data file
- Check
- After every change, check the output
- After this framework has been assembled, you should be ready
to pay attention to the operations specified by the
requirements.
- You can think of the items 1-8 here as laying a solid
foundation for the rest of your project.
Example GUI
import
javax.swing.*;
import
java.awt.*;
public
class GUI_01 extends JFrame {
public GUI_01 () {
this ("one");
} // end no parameter constructor
public GUI_01 (String t) {
setTitle (t);
setSize (200,200);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo (null);
add (new JButton ("North " + t), BorderLayout.NORTH);
add (new JButton ("East " + t), BorderLayout.EAST);
add (new JButton ("West " + t), BorderLayout.WEST);
//
validate ();
pack ();
setVisible (true);
} // end String constructor
public static void main (String args []) {
GUI_01 ga = new GUI_01 ();
GUI_01 gb = new GUI_01 ("two");
} // end main
}
// end class GUI_01
Example
Person/Student/Employee/Name
public
class School {
public static void main (String args []) {
Name n = new Name ();
Name m = new Name ("Jane", "Doe");
Student st = new Student ("Harry", "Potter", 38);
System.out.println ("Student: >" + n + "<");
System.out.println ("Student: >" + m + "<");
System.out.println ("Student: >" + st + "<");
System.out.println ("bye");
} // end main method
}
// end class School
class
Name {
String name = "Fred";
public Name () { }
public Name (String f, String l) {
name = f + " " + l;
} // end String String constructor
public String toString () {return name;}
}
// end class Name
class
Person {
}
// end class Person
class
Student extends Person {
Name n = new Name ("a", "b");
int credits;
public Student (String fn, String ln, int cr) {
credits = cr;
n = new Name (fn, ln);
} // end String,String,int constructor
public String toString () {
return n + " " + credits;
} // end method toString
}
// end class Student
class
Employee extends Person {
}
// end class Employee
By: Nicholas Duchon
Updated: May 16, 2017