The UML diagram, Figure 2.9, “UML Diagram of Class Inheritance” is a contract to be filled in by the programmer. Fill it in! This means
One possible solution would be:
Example C.3. Person.java
package lektion2;
/**
* @author nml
*
*/
public class Person {
protected Cprno cpr;
protected String name;
protected String address;
protected double balance;
public Person(Cprno cpr, String name, String address, double balance) {
this.cpr = cpr;
this.name = name;
this.address = address;
this.balance = balance;
}
public String getName() {
return this.name;
}
public String getAddress() {
return this.address;
}
public double getBalance() {
return this.balance;
}
public String getCpr() {
return this.cpr.toString();
}
public void addBalance(double amount) {
this.balance += amount;
}
public void setBalance(double amount) {
this.balance = amount;
}
@Override
public String toString() {
String s = this.name + " " + this.cpr.toString() + " " + this.address;
return s;
}
public static void main(String[] args) {
Cprno cpr1 = new Cprno(311882945);
Person p1 = new Person(cpr1, "Lucas", "Belleville", 0);
System.out.println(p1.toString());
}
}
Example C.4. Borrower.java
package lektion2;
public class Borrower extends Person {
private final boolean newsletter;
private final String email;
private final String[] interests;
public Borrower(Person p, boolean newsletter, String email,
String[] interests) {
super(p.cpr, p.name, p.address, p.balance);
this.newsletter = newsletter;
this.email = email;
this.interests = interests;
}
public String getEmail() {
return this.email;
}
public boolean wantsNewsletter() {
return this.newsletter;
}
public String showInterests() {
String s = "";
for (String interest : this.interests) {
s += interest;
}
return s;
}
public boolean hasInterest(String lookForInterest) {
boolean hasInterest = false;
for (String currentInterest : this.interests) {
if (lookForInterest == currentInterest) {
hasInterest = true;
}
}
return hasInterest;
}
public static void main(String[] args) {
Cprno cpr1 = new Cprno(311882945);
Person p1 = new Person(cpr1, "Lucas", "Belleville", 0);
String[] interests = { "i1", "i2", "i3" };
Borrower b1 = new Borrower(p1, false, "hahaha@wohoo.it", interests); // let's
// say
// the
// Person
// already
// exists..
System.out.println(b1.toString() + " " + b1.getEmail());
System.out.println("Interested in jogging? "
+ b1.hasInterest("jogging"));
System.out.println("Wants newsletters? " + b1.wantsNewsletter());
}
}
Example C.5. Librarian.java
package lektion2;
public class Librarian extends Person {
private final double empFrac;
private final String position;
public Librarian(Cprno cpr, String name, String address, double balance,
double empFrac, String position) {
super(cpr, name, address, balance);
this.empFrac = empFrac;
this.position = position;
}
public boolean isFullTime() { // ..I'm not fluent with the concept of
// employment fraction
if (this.empFrac >= 1) { // I consider full-time an employee with an
// empFrac >= 1
return true;
} else {
return false;
}
}
public String getPosition() {
return this.position;
}
@Override
public String toString() {
String s = this.name + " " + this.cpr.toString() + " " + this.position;
return s;
}
public static void main(String[] args) {
Cprno cpr1 = new Cprno(311882945);
Librarian l1 = new Librarian(cpr1, "Lucas", "Belleville", 0, 0.01,
"Keeper of the Key");
System.out.println(l1.toString());
System.out.println(String.format("is %s hired fulltime? %s", l1.name,
l1.isFullTime()));
}
}
Example C.6. Cprno.java
package lektion2;
public class Cprno {
private long cprno;
public Cprno() {
}
public Cprno(long cprno) {
this.cprno = cprno;
}
public long getCprno() {
return this.cprno;
}
public void setCprno(long cprno) {
this.cprno = cprno;
}
public boolean checkCprno() {
long sum;
long restcpr = this.cprno;
sum = 0L;
sum += restcpr % 10 * 1;
restcpr = restcpr / 10;
sum += restcpr % 10 * 2;
restcpr = restcpr / 10;
sum += restcpr % 10 * 3;
restcpr = restcpr / 10;
sum += restcpr % 10 * 4;
restcpr = restcpr / 10;
sum += restcpr % 10 * 5;
restcpr = restcpr / 10;
sum += restcpr % 10 * 6;
restcpr = restcpr / 10;
sum += restcpr % 10 * 7;
restcpr = restcpr / 10;
sum += restcpr % 10 * 2;
restcpr = restcpr / 10;
sum += restcpr % 10 * 3;
restcpr = restcpr / 10;
sum += restcpr % 10 * 4;
restcpr = restcpr / 10;
if (sum % 11 == 0 && sum != 0) {
return true;
} else {
return false;
}
}
@Override
public String toString() {
String aString = "";
long iLong;
iLong = this.cprno / 10000;
if (iLong < 100000)
aString += "0";
aString += Long.toString(iLong);
aString += "-";
iLong = this.cprno % 10000;
if (iLong < 1000)
aString += "0";
if (iLong < 100)
aString += "0";
if (iLong < 10)
aString += "0";
aString += Long.toString(iLong);
return aString;
}
}
For the hell of it, and in order for me to gauge your basic programming skills, and thoughts, please create
WordPlay.java
that will check a string for being a palindrome.
This program will need the following considerations and
code
first,
a method returning the first letter of the string
last, a method returning
the last character of the string
middle, returning
the middle part of the string, ie the string without
the first and last characters.
Hint: Check the Sun/Oracle documentation for the
String class and look for substring,
a useful method
isPalindrome,
having a string as input and returning a boolean value
containing the verdict on the input string. Write that
method
WordPlay.java before writing the utility methods.
Example C.7. Palindrome.java
package lektion2;
public class Palindrome {
public static final String RGX = "[ ,.:,-?!]"; // punctuation
private static char first(String word) {
return word.charAt(0);
}
private static char last(String word) {
return word.charAt(word.length() - 1);
}
private static String middle(String word) {
return word.substring(1, word.length() - 1);
}
/**
* This method prepares the string to be tested all chars to lower, all
* white space and punctuation removed. If done from caller it's done only
* once, if done from isPalindrome it's done on each recursive call
*/
public static String clean(String word) { // remove punctuation etc
word = word.toLowerCase();
return word.replaceAll(Palindrome.RGX, "");
}
public static boolean isPalindrome(String word) {
if (word.length() == 1)
return true;
else if (word.length() == 2)
return first(word) == last(word);
else {
System.out.println(first(word) + " - " + middle(word) + " - "
+ last(word));
return first(word) == last(word) && isPalindrome(middle(word));
}
}
}
Example C.8. WordPlay.java
package lektion2;
import javax.swing.JOptionPane;
/**
* @author nml
*
*/
public class WordPlay {
/**
* @param args
*/
public static void main(String[] args) {
String t;
String s = JOptionPane.showInputDialog("Enter a phrase: ");
String u = Palindrome.clean(s); // remove white noise
if (Palindrome.isPalindrome(u))
t = s + "\nis a palindrome";
else
t = s + "\nis not a palindrome";
JOptionPane.showMessageDialog(null, t);
System.out.println(t);
}
}