exercise sheet 2
Welcome to the Programming in Java Lab.
Task 1. Implement a date class b2. Date. As a constructor parameter expects day, month, and year as int values. A date once set should be immutable. In addition, the standard getter class for reading the data provide day, month, year elements of history. In addition, a formatted string representation of their content can be generated. Don’t forget the standard methods too also implement and realize a “copy constructor”. example:
Datum d1 = new Datum(17, 10, 2022);
System.out.println(d1.fmt()); // 17.10.2022
System.out.format(“%d.%d.%d\n”,
d1.getTag(), d1.getMonat(), d1.getJahr()); // 17.10.2022
System.out.println(d1); // Datum(tag=17, monat=10, jahr=2022)
Datum d2 = new Datum(17, 11, 2022);
System.out.format(“%s\n”, d1.equals(d2)); // false
Datum d3 = new Datum(17, 10, 2022);
System.out.format(“%s\n”, d1.equals(d3)); // true
System.out.format(“%s\n”, d1 == d3); // false
Datum d4 = d1;
System.out.format(“%s\n”, d1 == d4); // true
Set<Datum> set = new HashSet<>();
for (int i = 1; i <= 15; ++i) {
set.add(new Datum(i, 10, 2022));
set.add(new Datum(i + 1, 10, 2022));
set.add(new Datum(i + 2, 10, 2022));
}
System.out.format(“%d\n”, set.size()); // 1
Task 2. Add a method int getWeekday () to the date class returns the current day of the week. The number of the day of the week (0 = Saturday, 1 = Sunday, …, 6 = Friday). To do this, follow the steps below in this order through. If month = 2: increase day by 31.
The operator stands for multiplication, / means integer division and % of remainder or modulo. Add a new method String getWeekDayString() to the date class, which returns the day of the week as a string. Avoid unnecessary calculations and try to get by without control structures such as if or switch for the string output men. example:
Datum d1 = new Datum(17, 10, 2022);
System.out.print(d1.getWochentag() + “, “);
System.out.println(d1.getWochentagString()); // 2, Montag
for (int i = 0; i < 7; i += 1) {
Datum d = new Datum(17 + i, 10, 2022);
System.out.print(d.getWochentag());
}
System.out.println(); // 2345601
Task 3. A stamp card has a fixed number of fields. With each use, a field is “stamped” with the current date until it is full. Implement class b2. Stamp card that receives the number of stamp fields as a constructor parameter. With the method boolean stamp (date d) the next free field is always “stamped” with the transferred date and true is returned. Of course only if there is still space on the card, otherwise nothing happens and false is returned. With the method boolean cancel (), the last stamp is canceled (we assume that one has stamped by mistake), which can be done several times. True is returned if and only if (gdw) the cancellation was successful. Finally, there is a method int free fields () that returns the number of fields that are still free. The stamp card can be formatted as a string as shown in the example and two stamp cards can be tested against each other for equality. They are exactly the same if both the total number of fields and the stamped fields match in the order and content of the dates they contain. example:
Stempelkarte s1 = new Stempelkarte(5);
Stempelkarte s2 = new Stempelkarte(10);
System.out.format(“%s\n”, s1.equals(s2)); // false
Stempelkarte s3 = new Stempelkarte(5);
System.out.format(“%s\n”, s1.equals(s3)); // true
Datum d1 = new Datum(17, 10, 2022);
s1.abstempeln(d1);
System.out.format(“%s\n”, s1.equals(s3)); // false
s3.abstempeln(d1);
System.out.format(“%s\n”, s1.equals(s3)); // true
s1.abstempeln(new Datum(18, 10, 2022));
System.out.format(“%d\n”, s1.freieFelder()); // 3
System.out.format(“%s\n”, s1.stornieren()); // true
System.out.format(“%s\n”, s1.toString());
// Stempelkarte(5, [17.10.2022, …]-4)
Note 1. For all tasks, see the B2Test file. java a collection of unit tests. Use the quizzes to test your solution to the tasks. If you have read through the entire exercise sheet beforehand, as originally planned, then you can of course also start with the tests.
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more
Recent Comments