├── src
└── gr
│ └── aueb
│ ├── cf
│ ├── ch16
│ │ ├── ISpeakable.java
│ │ ├── products
│ │ │ ├── IMilk.java
│ │ │ ├── Coffee.java
│ │ │ ├── Honey.java
│ │ │ ├── IGenericProduct.java
│ │ │ └── Milk.java
│ │ ├── marker
│ │ │ ├── Item.java
│ │ │ ├── Main.java
│ │ │ └── CompactDisk.java
│ │ ├── multipleinh
│ │ │ ├── ITalking.java
│ │ │ ├── IReadable.java
│ │ │ ├── ISpeakable.java
│ │ │ └── TalkingBook.java
│ │ ├── skeletal
│ │ │ ├── IRectangle.java
│ │ │ ├── Main.java
│ │ │ ├── Square.java
│ │ │ └── AbstractRectangle.java
│ │ ├── SpeakingSchool.java
│ │ ├── GenericSpeakingSchool.java
│ │ ├── abstractclasses
│ │ │ ├── Cat.java
│ │ │ └── Animal.java
│ │ ├── SchoolApp.java
│ │ ├── Main.java
│ │ ├── Cat.java
│ │ └── Dog.java
│ ├── solutions
│ │ ├── ch1
│ │ │ ├── img.png
│ │ │ ├── MenuApp.java
│ │ │ ├── InitialsApp.java
│ │ │ ├── SumApp.java
│ │ │ ├── readme.md
│ │ │ └── hints.md
│ │ ├── ch3
│ │ │ ├── img.png
│ │ │ ├── readme.md
│ │ │ ├── LeapYearApp.java
│ │ │ └── Menu2App.java
│ │ ├── ch10
│ │ │ └── readme.md
│ │ ├── ch4
│ │ │ └── readme.md
│ │ ├── ch7
│ │ │ └── readme.md
│ │ ├── ch6
│ │ │ ├── readme.md
│ │ │ └── MaxPositionApp.java
│ │ ├── ch5
│ │ │ └── readme.md
│ │ ├── ch2
│ │ │ ├── FahrenheitToCelsiusApp.java
│ │ │ ├── DateApp.java
│ │ │ └── readme.md
│ │ └── ch8
│ │ │ └── readme.md
│ ├── ch15
│ │ ├── products
│ │ │ └── model
│ │ │ │ ├── IMilk.java
│ │ │ │ ├── IProduct.java
│ │ │ │ ├── Honey.java
│ │ │ │ └── Milk.java
│ │ ├── accounts
│ │ │ └── model
│ │ │ │ ├── IdentifiableEntity.java
│ │ │ │ ├── OverdraftAccount.java
│ │ │ │ └── OverdraftJointAccount.java
│ │ ├── PointApp.java
│ │ ├── Point2D.java
│ │ ├── Point3D.java
│ │ ├── Point.java
│ │ └── PointsDemo.java
│ ├── ch17
│ │ ├── threads
│ │ │ ├── MissionStatus.java
│ │ │ ├── IMission.java
│ │ │ ├── IKnight.java
│ │ │ ├── Main.java
│ │ │ ├── KillTheDragonMission.java
│ │ │ ├── SaveThePrincess.java
│ │ │ └── Knight.java
│ │ ├── knight
│ │ │ ├── IMission.java
│ │ │ ├── IKnight.java
│ │ │ ├── Knight.java
│ │ │ └── PlayTheGame.java
│ │ ├── Main.java
│ │ └── clone
│ │ │ └── Main.java
│ ├── ch18
│ │ ├── model
│ │ │ ├── IdentifiableEntity.java
│ │ │ └── AbstractEntity.java
│ │ ├── dto
│ │ │ ├── BaseDTO.java
│ │ │ ├── UserDetailsInsertDTO.java
│ │ │ ├── UserDetailsUpdateDTO.java
│ │ │ ├── MobileContactInsertDTO.java
│ │ │ └── MobileContactUpdateDTO.java
│ │ ├── service
│ │ │ ├── exceptions
│ │ │ │ ├── UserIdAlreadyExistsException.java
│ │ │ │ ├── PhoneNumberAlreadyExistsException.java
│ │ │ │ └── ContactNotFoundException.java
│ │ │ └── IMobileContactService.java
│ │ └── dao
│ │ │ └── IMobileContactDAO.java
│ ├── ch20
│ │ ├── lambdas
│ │ │ └── ITeacherIDFilter.java
│ │ └── regex
│ │ │ ├── PasswordApp.java
│ │ │ ├── NonCapturingGroupsApp.java
│ │ │ ├── GreedyApp.java
│ │ │ └── DateApp.java
│ ├── ch2
│ │ ├── RandomDemo.java
│ │ ├── RandomDiceApp.java
│ │ ├── MathApp.java
│ │ ├── AddBigIntsApp.java
│ │ ├── MathPowersApp.java
│ │ ├── ScannerApp.java
│ │ ├── ExprApp.java
│ │ ├── AddApp.java
│ │ ├── IntApp.java
│ │ ├── BankOfScotlandApp.java
│ │ └── DateToSecondsApp.java
│ ├── ch11
│ │ ├── model
│ │ │ ├── package-info.java
│ │ │ ├── ImmutablePoint.java
│ │ │ └── ImmutablePoint3D.java
│ │ └── StudentApp.java
│ ├── sock
│ │ ├── EchoServerApp.java
│ │ ├── IterativeDayTimeServerApp.java
│ │ ├── DateTimeServ.java
│ │ ├── EchoCli.java
│ │ └── DateTimeCli.java
│ ├── ch19
│ │ ├── Node.java
│ │ ├── FlexibleNode.java
│ │ ├── GenericNode.java
│ │ ├── Main.java
│ │ ├── cfcollections
│ │ │ ├── SetApp.java
│ │ │ ├── StackApp.java
│ │ │ ├── QueueApp.java
│ │ │ ├── MyQueue.java
│ │ │ ├── MyStack.java
│ │ │ └── Main.java
│ │ ├── maps
│ │ │ ├── MapDemo2.java
│ │ │ ├── MapDemo.java
│ │ │ └── Main.java
│ │ ├── dlist
│ │ │ └── Node.java
│ │ └── GenericApp.java
│ ├── ch4
│ │ ├── ForBreakApp.java
│ │ ├── For10App.java
│ │ ├── Stars10.java
│ │ ├── Stars10Vertical.java
│ │ ├── IOApp.java
│ │ ├── OrdinalApp.java
│ │ ├── StarsHV10.java
│ │ ├── ForEverApp.java
│ │ ├── ForMulSumApp.java
│ │ ├── StarsAsc10.java
│ │ ├── IO2App.java
│ │ ├── StarsDesc10.java
│ │ ├── ForPowerApp.java
│ │ ├── EmojisApp.java
│ │ ├── ForBigIntApp.java
│ │ ├── HelloChar.java
│ │ ├── ForFlexibleApp.java
│ │ ├── SwitchFallThroughApp.java
│ │ └── SwitchApp.java
│ ├── ch3
│ │ ├── Stars10App.java
│ │ ├── Sum10.java
│ │ ├── Mul10App.java
│ │ ├── BooleanApp.java
│ │ ├── FactorialApp.java
│ │ ├── IfPositiveApp.java
│ │ ├── SpecialWhileApp.java
│ │ ├── StarsGenericApp.java
│ │ ├── MenuApp.java
│ │ ├── MulGenApp.java
│ │ ├── SumGenApp.java
│ │ ├── TemperatureApp.java
│ │ ├── DigitCountApp.java
│ │ ├── Bingo2IfApp.java
│ │ ├── PowerApp.java
│ │ ├── IfAbsApp.java
│ │ ├── PositiveCountApp.java
│ │ ├── IfGradesApp.java
│ │ ├── SnowingApp.java
│ │ ├── MinValueApp.java
│ │ ├── BingoIfApp.java
│ │ ├── IfTaxApp.java
│ │ ├── WhileApp.java
│ │ ├── Division2App.java
│ │ ├── GenericWhileApp.java
│ │ ├── TankApp.java
│ │ ├── DivisionApp.java
│ │ ├── IfAvgApp.java
│ │ ├── LightsOnApp.java
│ │ └── Menu1App.java
│ ├── ch1
│ │ ├── HelloWorld.java
│ │ └── AddApp.java
│ ├── ch14
│ │ ├── Main.java
│ │ ├── HelloUtil.java
│ │ ├── CodingFactory.java
│ │ ├── ImmutablePoint.java
│ │ ├── Point.java
│ │ └── FlyweightApp.java
│ ├── ch7
│ │ ├── StringFormatApp.java
│ │ ├── SplitApp.java
│ │ ├── ConcatApp.java
│ │ ├── HelloString.java
│ │ ├── PrintWithSubstring.java
│ │ ├── StringAccessApp.java
│ │ ├── StringSearchApp.java
│ │ ├── IOApp.java
│ │ ├── EqualityApp.java
│ │ └── StringBuilderApp.java
│ ├── ch6
│ │ ├── ArrayInitializer.java
│ │ ├── Array2App.java
│ │ ├── ArrayApp.java
│ │ ├── ArrayMinApp.java
│ │ ├── BubbleSort.java
│ │ ├── ArraysUtilCopy.java
│ │ ├── SwapApp.java
│ │ ├── ArrayCopy.java
│ │ ├── ArraySearchApp.java
│ │ ├── ReplaceArrayElements.java
│ │ ├── SymmetricArray.java
│ │ ├── SelectionSort.java
│ │ ├── CircularArrayRotation.java
│ │ ├── Predicates.java
│ │ ├── ArrayMin2App.java
│ │ ├── ReducingApp.java
│ │ ├── MappingApp.java
│ │ ├── BinarySearch.java
│ │ ├── BitwiseApp.java
│ │ ├── TwoDimArray.java
│ │ ├── ArrayAddOneApp.java
│ │ ├── Filtering.java
│ │ └── MethodsWithArrays.java
│ ├── ch5
│ │ ├── PowerRecursive.java
│ │ ├── FloatingPointApp.java
│ │ ├── OverloadingApp.java
│ │ ├── FactoRecursive.java
│ │ ├── CompareWithEpsilon.java
│ │ ├── CompareWithoutEpsilon.java
│ │ ├── SwapApp.java
│ │ ├── FactorialApp.java
│ │ ├── BigDecimalApp.java
│ │ ├── EvenOrOddApp.java
│ │ ├── AddApp.java
│ │ └── PowerApp.java
│ ├── ch10
│ │ ├── FrogJumps2.java
│ │ ├── SmallestPositive2.java
│ │ ├── MissingArrayElement2.java
│ │ └── NotPaired2.java
│ ├── ch9
│ │ ├── PrintStreamApp.java
│ │ ├── PrintStreamAutoFlushApp.java
│ │ ├── BufferedReaderApp.java
│ │ ├── FlushApp.java
│ │ ├── IOCopy3.java
│ │ ├── IOCopy2.java
│ │ ├── IOCopy1.java
│ │ └── PrintStreamMethod.java
│ └── ch8
│ │ ├── ArithmeticExceptionApp.java
│ │ ├── TryWithResourcesApp.java
│ │ ├── StringsWithoutTryCatchApp.java
│ │ ├── InputMismatchException2App.java
│ │ ├── InputMismatchExceptionApp.java
│ │ ├── FinallyApp.java
│ │ ├── CharReadApp.java
│ │ ├── MultipleExceptionsApp.java
│ │ ├── NumberFormatExceptionApp.java
│ │ └── ArithmeticException2App.java
│ └── cf6
│ ├── ch7
│ ├── CopyStr_9.java
│ ├── TraverseApp_3.java
│ ├── StrFormatApp_12.java
│ ├── TraverseWithSubstring_5.java
│ ├── ParseInt_10.java
│ ├── SplitApp_11.java
│ ├── CompareStr_8.java
│ ├── StringHello_1.java
│ ├── EqualsStr_7.java
│ ├── Concat_6.java
│ ├── ReadString_2.java
│ ├── IndexOfSubstring_4.java
│ └── StrBuilderApp_13.java
│ └── ch6
│ ├── PopulateApp_1.java
│ ├── SymmetricArray_16.java
│ ├── Swap_4.java
│ ├── MappingApp_7.java
│ ├── ReducingApp_11.java
│ ├── ArrayUtil_13.java
│ ├── FilteringApp_6.java
│ ├── SelectionSort_5.java
│ ├── BinarySearch_12.java
│ ├── TwoDim_14.java
│ ├── SearchArray_2.java
│ └── StackApp_9.java
└── README.md
/src/gr/aueb/cf/ch16/ISpeakable.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public interface ISpeakable {
4 | void speak();
5 | }
6 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/products/IMilk.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.products;
2 |
3 | public interface IMilk {
4 | Milk get();
5 | }
6 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/a8anassis/codingfactory5-java/HEAD/src/gr/aueb/cf/solutions/ch1/img.png
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch3/img.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/a8anassis/codingfactory5-java/HEAD/src/gr/aueb/cf/solutions/ch3/img.png
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/products/Coffee.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.products;
2 |
3 | public class Coffee extends AbstractGenericProduct {
4 | }
5 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/products/Honey.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.products;
2 |
3 | public class Honey extends AbstractGenericProduct {
4 | }
5 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/products/model/IMilk.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.products.model;
2 |
3 | public interface IMilk {
4 | Milk get();
5 | }
6 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/marker/Item.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.marker;
2 |
3 | /**
4 | * Marker interface.
5 | */
6 | public interface Item {
7 | }
8 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/multipleinh/ITalking.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.multipleinh;
2 |
3 | public interface ITalking extends ISpeakable, IReadable {
4 | }
5 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/products/IGenericProduct.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.products;
2 |
3 | public interface IGenericProduct {
4 | void insert();
5 | }
6 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/MissionStatus.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public enum MissionStatus {
4 | STARTED,
5 | NOT_STARTED
6 | }
7 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/model/IdentifiableEntity.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.model;
2 |
3 | public interface IdentifiableEntity {
4 | Long getId();
5 | }
6 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/knight/IMission.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.knight;
2 |
3 | @FunctionalInterface
4 | public interface IMission {
5 | void embark();
6 | }
7 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/multipleinh/IReadable.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.multipleinh;
2 |
3 | @FunctionalInterface
4 | public interface IReadable {
5 | void read();
6 | }
7 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/multipleinh/ISpeakable.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.multipleinh;
2 |
3 | @FunctionalInterface
4 | public interface ISpeakable {
5 | void speak();
6 | }
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # codingfactory5-java
2 | This is the main repository for CF5 (start October 2023) java material. It includes mini-projects organized in chapters, challenges and their solutions .
3 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch10/readme.md:
--------------------------------------------------------------------------------
1 | **Challenge Projects**
2 |
3 | 1. Lotto App.
4 | 2. Max Sub-array
5 | 3. Shallow vs Deep 2D Array copy.
6 | 4. Tic-tac-toe game.
7 | 5. Theater App
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/knight/IKnight.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.knight;
2 |
3 | @FunctionalInterface
4 | public interface IKnight {
5 |
6 | void embarkOnMission(IMission mission);
7 | }
8 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch20/lambdas/ITeacherIDFilter.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch20.lambdas;
2 |
3 | @FunctionalInterface
4 | public interface ITeacherIDFilter {
5 | boolean filterId(Teacher teacher);
6 | }
7 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/skeletal/IRectangle.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.skeletal;
2 |
3 | public interface IRectangle {
4 | double getArea();
5 | double getPerimeter();
6 | boolean isSquare();
7 | }
8 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/IMission.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public interface IMission {
4 | MissionStatus getStatus();
5 | void setStatus(MissionStatus status);
6 | void embark();
7 | }
8 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/products/model/IProduct.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.products.model;
2 |
3 | public interface IProduct {
4 |
5 | /**
6 | * Inserts a product in a storage.
7 | */
8 | void insert();
9 | }
10 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/RandomDemo.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | public class RandomDemo {
4 | public static void main(String[] args) {
5 | int die = (int) (Math.random() * 6) + 1;
6 | System.out.println(die);
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/knight/Knight.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.knight;
2 |
3 | public class Knight implements IKnight {
4 |
5 | @Override
6 | public void embarkOnMission(IMission mission) {
7 | mission.embark();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch11/model/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * This package includes the following model classes:
3 | *
4 | * {@link gr.aueb.cf.ch11.model.ImmutablePoint}
5 | * {@link gr.aueb.cf.ch11.model.ImmutablePoint3D}
6 | */
7 | package gr.aueb.cf.ch11.model;
--------------------------------------------------------------------------------
/src/gr/aueb/cf/sock/EchoServerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.sock;
2 |
3 | public class EchoServerApp {
4 |
5 | public static void main(String[] args) {
6 | EchoServer echoServer = new EchoServer();
7 | echoServer.start();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch4/readme.md:
--------------------------------------------------------------------------------
1 | **Άσκηση 1**
2 |
3 | Αναπτύξτε πέντε προγράμματα αντίστοιχα με τα προηγούμενα όπου ο χρήστης θα δίνει το πλήθος
4 | των stars, έστω n.
5 | 1. n οριζόντια αστεράκια,
6 | 2. n κάθετα,
7 | 3. nxn,
8 | 4. από 1 έως n,
9 | 5. από n έως 1
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/Node.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19;
2 |
3 | public class Node {
4 | private int item;
5 |
6 | public int getItem() {
7 | return item;
8 | }
9 |
10 | public void setItem(int item) {
11 | this.item = item;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dto/BaseDTO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dto;
2 |
3 | public abstract class BaseDTO {
4 | private Long id;
5 |
6 | public Long getId() {
7 | return id;
8 | }
9 |
10 | public void setId(Long id) {
11 | this.id = id;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/sock/IterativeDayTimeServerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.sock;
2 |
3 | public class IterativeDayTimeServerApp {
4 |
5 | public static void main(String[] args) {
6 | DateTimeServ dateTimeServ = new DateTimeServ();
7 | dateTimeServ.start();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/SpeakingSchool.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class SpeakingSchool {
4 | private final Cat cat;
5 |
6 | public SpeakingSchool() {
7 | cat = new Cat();
8 | }
9 |
10 | public void learnToSpeak() {
11 | cat.speak();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/FlexibleNode.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19;
2 |
3 | public class FlexibleNode {
4 | private Object item;
5 |
6 | public Object getItem() {
7 | return item;
8 | }
9 |
10 | public void setItem(Object item) {
11 | this.item = item;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForBreakApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | public class ForBreakApp {
4 |
5 | public static void main(String[] args) {
6 |
7 | for (int i = 1; i <= 10; i++) {
8 | System.out.print(i + " ");
9 | if (i == 5) break;
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/For10App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εκτελεί 10 επαναλήψεις με for.
5 | */
6 | public class For10App {
7 |
8 | public static void main(String[] args) {
9 |
10 | for (int i = 1; i <= 10; i++) {
11 | System.out.print(i + " ");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/Stars10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εκτυπώνει 10 οριζόντια αστεράκια.
5 | */
6 | public class Stars10 {
7 |
8 | public static void main(String[] args) {
9 |
10 | for (int i = 1; i <= 10; i++) {
11 | System.out.print("*");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/CopyStr_9.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class CopyStr_9 {
4 |
5 | public static void main(String[] args) {
6 | String s = "Athens";
7 | String copyOfS = copyStr(s);
8 | }
9 |
10 | public static String copyStr(String s) {
11 | return s;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/Stars10Vertical.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εκτυπώνει 10 κάθετα stars.
5 | */
6 | public class Stars10Vertical {
7 |
8 | public static void main(String[] args) {
9 |
10 | for (int i = 1; i <= 10; i++) {
11 | System.out.println("*");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/TraverseApp_3.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class TraverseApp_3 {
4 |
5 | public static void main(String[] args) {
6 | String s = "Coding Factory";
7 |
8 | for (int i = 0; i < s.length(); i++) {
9 | System.out.print(s.charAt(i) + " ");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/model/AbstractEntity.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.model;
2 |
3 | public abstract class AbstractEntity implements IdentifiableEntity {
4 | private Long id;
5 |
6 | @Override
7 | public Long getId() {
8 | return id;
9 | }
10 |
11 | public void setId(Long id) {
12 | this.id = id;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/StrFormatApp_12.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class StrFormatApp_12 {
4 |
5 | public static void main(String[] args) {
6 | char row = 'A';
7 | int column = 1;
8 |
9 | String seat = String.format("%c%02d", row, column);
10 | System.out.println("Seat: " + seat);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Stars10App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | /**
4 | * Εμφανίζει 10 οριζόντια αστεράκια!
5 | */
6 | public class Stars10App {
7 |
8 | public static void main(String[] args) {
9 | int i = 10;
10 |
11 | while (i >= 1) {
12 | System.out.print("*");
13 | i--;
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/IKnight.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | @FunctionalInterface
4 | public interface IKnight {
5 | IMission[] missions = {
6 | new KillTheDragonMission(),
7 | new SaveThePrincess(),
8 | new KillTheDragonMission()
9 | };
10 |
11 | void embarkOnMission(IMission mission);
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/GenericSpeakingSchool.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class GenericSpeakingSchool {
4 | private final ISpeakable speakable;
5 |
6 | public GenericSpeakingSchool(ISpeakable speakable) {
7 | this.speakable = speakable;
8 | }
9 |
10 | public void learnToSpeak() {
11 | speakable.speak();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch1/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch1;
2 |
3 | /**
4 | * Εκτυπώνει στην κονσόλα (std output)
5 | * τη φράση Hello Coding Factory.
6 | *
7 | * @author a8ana
8 | */
9 | public class HelloWorld {
10 | public static void main(String[] args) {
11 | System.out.println("Hello World");
12 | // System.out.println();
13 | }
14 | }
15 |
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/skeletal/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.skeletal;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | //Square square = new Square(2, 4);
7 | Square square = new Square();
8 | square.setHeight(2);
9 | square.setWidth(4);
10 |
11 | System.out.println(square);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | CodingFactory codingFactory = CodingFactory.getInstance();
7 | CodingFactory codingFactory2 = CodingFactory.getInstance();
8 | codingFactory.sayHi("CF Hi!");
9 |
10 | HelloUtil.sayHello();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/accounts/model/IdentifiableEntity.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.accounts.model;
2 |
3 | public class IdentifiableEntity {
4 | private Long id;
5 |
6 | public IdentifiableEntity() {
7 |
8 | }
9 |
10 | public Long getId() {
11 | return id;
12 | }
13 |
14 | public void setId(Long id) {
15 | this.id = id;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/multipleinh/TalkingBook.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.multipleinh;
2 |
3 | public class TalkingBook implements ISpeakable, IReadable {
4 |
5 | @Override
6 | public void read() {
7 | System.out.println("Can read!");
8 | }
9 |
10 | @Override
11 | public void speak() {
12 | System.out.println("Can speak!");
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/IOApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.io.IOException;
4 |
5 | public class IOApp {
6 |
7 | public static void main(String[] args) throws IOException {
8 | char ch = ' ';
9 |
10 | System.out.println("Please insert a char");
11 | ch = (char) System.in.read();
12 |
13 | System.out.println(ch);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/StringFormatApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Demo of String format
5 | */
6 | public class StringFormatApp {
7 |
8 | public static void main(String[] args) {
9 | char row = 'G';
10 | int column = 8;
11 |
12 | String seat = String.format("%c%02d", row, column);
13 | System.out.println(seat);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayInitializer.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Array Initializer.
5 | */
6 | public class ArrayInitializer {
7 |
8 | public static void main(String[] args) {
9 | int[] arr;
10 | arr = new int[] {1, 2, 3, 4, 5, 6};
11 |
12 | for (int num : arr) {
13 | System.out.print(num + " ");
14 | }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/abstractclasses/Cat.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.abstractclasses;
2 |
3 | public class Cat extends Animal {
4 |
5 |
6 | @Override
7 | public void speak() {
8 | System.out.println("Niaou");
9 | }
10 |
11 | @Override
12 | public void eat() {
13 | super.eat();
14 | System.out.println(" ... cat ate all her food");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/PowerRecursive.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class PowerRecursive {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 | public static int power(int a, int n) {
10 | /*if (n == 0) {
11 | return 1;
12 | }
13 |
14 | return a * power(a, n-1);*/
15 |
16 | return (n == 0) ? 1 : a * power(a, n-1);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/RandomDiceApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | /**
4 | * Προσομοιώνουμε δύο ζάρια.
5 | */
6 | public class RandomDiceApp {
7 |
8 | public static void main(String[] args) {
9 | int die1 = (int) (Math.random() * 6) + 1; // * (max - min + 1) + min
10 | int die2 = (int) (Math.random() * 6) + 1;
11 |
12 | System.out.println("Die1: " + die1 + ", Die2: " + die2);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/SplitApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Demo of Split method.
5 | */
6 | public class SplitApp {
7 |
8 | public static void main(String[] args) {
9 | String aueb = "Athens::::::::Un:::Of:Econ::::and:Bus";
10 |
11 | String[] tokens = aueb.split(":");
12 |
13 | for (String token : tokens) {
14 | System.out.print(token);
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/TraverseWithSubstring_5.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class TraverseWithSubstring_5 {
4 |
5 | public static void main(String[] args) {
6 | String s = "Athens";
7 | traverse(s);
8 | }
9 |
10 | public static void traverse(String s) {
11 | for (int i = 0; i < s.length(); i++) {
12 | System.out.print(s.substring(i, i+1));
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/MathApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | /**
4 | * Demonstrates the Math class.
5 | */
6 | public class MathApp {
7 | public static void main(String[] args) {
8 | System.out.println("Το απόλυτο του -5 είναι: " + Math.abs(-5));
9 | System.out.println("Το ελάχιστο του 2, 7 είναι: " + Math.min(2, 7));
10 | System.out.println("Το max του 2, 7 είναι: " + Math.max(2, 7));
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/OrdinalApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Αντιστοίχιση char - ordinal numbers
5 | */
6 | public class OrdinalApp {
7 |
8 | public static void main(String[] args) {
9 | char beta = 'B';
10 | char bang = '!';
11 |
12 | System.out.printf("char: %c, ordinal: %d\n", beta, (int) beta);
13 | System.out.printf("char: %c, ordinal: %d", bang, (int) bang);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/StarsHV10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εκτυπώνει 10 γραμμές με 10 stars η κάθε γραμμή.
5 | */
6 | public class StarsHV10 {
7 |
8 | public static void main(String[] args) {
9 |
10 | for (int i = 1; i <= 10; i++) {
11 | for (int j = 1; j <= 10; j++) {
12 | System.out.print("*");
13 | }
14 | System.out.println();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/HelloUtil.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | /**
4 | * Utility Class.
5 | * -private constructor
6 | * -no state
7 | * -public static methods
8 | */
9 | public class HelloUtil {
10 |
11 | /**
12 | * No instances of this class should be available.
13 | */
14 | private HelloUtil() {
15 |
16 | }
17 |
18 | public static void sayHello() {
19 | System.out.println("Hello CF!");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/GenericNode.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19;
2 |
3 | public class GenericNode {
4 | private T item;
5 |
6 | public T getItem() {
7 | return item;
8 | }
9 |
10 | public void setItem(T item) {
11 | this.item = item;
12 | }
13 |
14 | @Override
15 | public String toString() {
16 | return "GenericNode{" +
17 | "item=" + item +
18 | '}';
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Sum10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | /**
4 | * Υπολογίζει το 1 + 2 + ... + 10
5 | * με τη while..do.
6 | */
7 | public class Sum10 {
8 |
9 | public static void main(String[] args) {
10 | int i = 1;
11 | int result = 0;
12 |
13 | while (i <= 10) {
14 | result = result + i;
15 | i++;
16 | }
17 |
18 | System.out.println("Sum: " + result);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/ConcatApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Demonstrates concat
5 | */
6 | public class ConcatApp {
7 |
8 | public static void main(String[] args) {
9 | String s1 = "Coding";
10 | String s2 = "Factory";
11 | String myString; // null
12 |
13 | String s3 = s1.concat(" ").concat(s2);
14 | String s4 = s1 + " " + s2;
15 | //myString.concat("a");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/skeletal/Square.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.skeletal;
2 |
3 | public class Square extends AbstractRectangle {
4 |
5 |
6 | }
7 |
8 |
9 | // public Square() {
10 | //
11 | // }
12 | //
13 | // public Square(double x, double y) {
14 | // super(x, y);
15 | // }
16 | //
17 | // @Override
18 | // public String toString() {
19 | // return "(" + getWidth() + " , " + getHeight() + ")";
20 | // }
21 | // }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForEverApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Eternal for.
5 | */
6 | public class ForEverApp {
7 |
8 | public static void main(String[] args) {
9 | int counter = 0;
10 |
11 | for (;;) {
12 | counter++;
13 | System.out.print("CF\u2661Forever");
14 | if (counter % 20 == 0) System.out.println();
15 | if (counter == 100) break;
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch10/FrogJumps2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch10;
2 |
3 | public class FrogJumps2 {
4 |
5 | public static void main(String[] args) {
6 | int x = 10;
7 | int y = 85;
8 | int d = 20;
9 |
10 | System.out.println(getJumps(x, y, d));
11 | }
12 |
13 | public static int getJumps(int startPos, int endPos, int jmp) {
14 |
15 | return (int) Math.ceil((endPos - startPos) / (double) jmp);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/SchoolApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class SchoolApp {
4 |
5 | public static void main(String[] args) {
6 | ISpeakable whiteCat = new Cat();
7 | ISpeakable blackDog = new Dog();
8 |
9 | // Wiring
10 | GenericSpeakingSchool aliceSpeakingSchool = new GenericSpeakingSchool(whiteCat);
11 | GenericSpeakingSchool bobSpeakingSchool = new GenericSpeakingSchool(blackDog);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | FlexibleNode flexibleNode1 = new FlexibleNode();
7 | flexibleNode1.setItem("Hello");
8 |
9 | FlexibleNode flexibleNode2 = new FlexibleNode();
10 | flexibleNode2.setItem(2);
11 |
12 | int myInt = (int) flexibleNode1.getItem();
13 |
14 | System.out.println(myInt);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/SetApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | public class SetApp {
7 |
8 | public static void main(String[] args) {
9 | Set bag = new HashSet<>();
10 | bag.add("Apples");
11 | bag.add("Oranges");
12 | bag.remove("Apples");
13 | bag.add("Oranges");
14 |
15 | bag.forEach(System.out::println);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch7/readme.md:
--------------------------------------------------------------------------------
1 | **Encryption / Decryption**
2 |
3 | Δημιουργήστε δύο προγράμματα, ένα για κρυπτογράφηση και ένα για αποκρυπτογράφηση:
4 |
5 | * Κρυπτογράφηση: Ένα πρόγραμμα που να κρυπτογραφεί / αντικαθιστά κάθε χαρακτήρα του
6 | String με τον λεξικογραφικά (ASCII-κογραφικα!) επόμενο
7 |
8 | * Αποκρυπτογράφηση: Ένα πρόγραμμα που να αποκρυπτογραφεί / αντικαθιστά
9 | κάθε χαρακτήρα του String με τον λεξικογραφικά (ASCII-κογραφικα!) προηγούμενο
10 |
11 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/AddBigIntsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 |
4 | import java.math.BigInteger;
5 |
6 | public class AddBigIntsApp {
7 |
8 | public static void main(String[] args) {
9 | BigInteger bigNum1 = new BigInteger("213457586789555555555555555555555555555555");
10 | BigInteger bigNum2 = new BigInteger("12345678999994");
11 | BigInteger result = bigNum1.add(bigNum2) ;
12 |
13 | System.out.printf("%,d%n", result);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Mul10App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | /**
4 | * Υπολογίζει το 1 * 2 * 3 * ... * 10
5 | */
6 | public class Mul10App {
7 | public static void main(String[] args) {
8 | int i = 1;
9 | int result = 1;
10 |
11 | while (i <= 10) {
12 | result *= i;
13 | i++;
14 | }
15 |
16 | System.out.println("Result: " + result);
17 | System.out.printf("Result: %,d ", result);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch6/readme.md:
--------------------------------------------------------------------------------
1 | **Get Max Position**
2 |
3 | Αναπτύξτε μία γενική μέθοδο εύρεσης του μέγιστου ενός πίνακα getMaxPosition (int[]
4 | arr, int low, int high) που επιστρέφει τη θέση στον πίνακα arr του μέγιστου
5 | στοιχείου του πίνακα
6 |
7 | **Second Min**
8 |
9 | Αναπτύξτε ένα πρόγραμμα που βρίσκει το 2ο μικρότερο στοιχείο ενός πίνακα, το
10 | στοιχείο δηλαδή που είναι αμέσως μεγαλύτερο από το ελάχιστο στοιχείο του πίνακα.
11 | Μη χρησιμοποιήσετε ταξινόμηση.
12 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForMulSumApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Υπολογίζει το άθροισμα και το γινόμενο
5 | * 1 έως 10.
6 | */
7 | public class ForMulSumApp {
8 |
9 | public static void main(String[] args) {
10 |
11 | int sum = 0;
12 | int mul = 1;
13 |
14 | for (int i = 1; i <= 10; i++) {
15 | sum += i;
16 | mul *= i;
17 | }
18 |
19 | System.out.printf("Sum: %d, Mul: %,d", sum, mul);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/StarsAsc10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εκτυπώνει 1 star στη 1η γραμμή, 2 stars στη 2η γραμμή,
5 | * 3 stars στη 3η γραμμή, κ.ο.κ.
6 | */
7 | public class StarsAsc10 {
8 |
9 | public static void main(String[] args) {
10 |
11 | for (int i = 1; i <= 10; i++) {
12 | for (int j = 1; j <= i; j++) {
13 | System.out.print("*");
14 | }
15 | System.out.println();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/HelloString.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | public class HelloString {
4 |
5 | public static void main(String[] args) {
6 | String alice = "Alice";
7 | String bob = "Bob";
8 |
9 | System.out.println(alice + " " + bob);
10 | System.out.printf("%s %s", alice, bob);
11 | System.out.println("Total length of alice string: " + alice.length());
12 | System.out.println("Total Bob length: " + bob.length());
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/ParseInt_10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ParseInt_10 {
6 |
7 | public static void main(String[] args) {
8 | String s;
9 | int num;
10 | Scanner in = new Scanner(System.in);
11 |
12 | System.out.println("Insert an int");
13 | s = in.nextLine();
14 | num = Integer.parseInt(s); // NumberFormatException
15 |
16 | System.out.println(num);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/service/exceptions/UserIdAlreadyExistsException.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.service.exceptions;
2 |
3 | import gr.aueb.cf.ch18.model.MobileContact;
4 |
5 | public class UserIdAlreadyExistsException extends Exception {
6 | private static final long serialVersionUID = 234562997373773378L;
7 |
8 | public UserIdAlreadyExistsException(MobileContact mobileContact) {
9 | super("Mobile contact with id:" + mobileContact.getId() + " already exists");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/Array2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Unsized initialization.
5 | */
6 | public class Array2App {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {1, 2, 3, 4, 5};
10 |
11 | // Traverse
12 | for (int i = 0; i <= arr.length; i++) {
13 | System.out.print(arr[i] + " ");
14 | }
15 |
16 | for (int num : arr) {
17 | System.out.print(num + " ");
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/CodingFactory.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | /**
4 | * Singleton Design Pattern.
5 | */
6 | public class CodingFactory {
7 | private static final CodingFactory INSTANCE = new CodingFactory();
8 |
9 | private CodingFactory() {
10 | }
11 |
12 | public static CodingFactory getInstance() {
13 | return INSTANCE;
14 | }
15 |
16 | public void sayHi(String message) {
17 | System.out.println("Hi Only One Coding Factory!");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/FloatingPointApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | /**
4 | * Float / Double Demo.
5 | */
6 | public class FloatingPointApp {
7 |
8 | public static void main(String[] args) {
9 | float floatNum = 4.5F;
10 | double doubleNum = 20d;
11 | int num = 23;
12 | double result = 0.0;
13 |
14 | result = floatNum + doubleNum + num;
15 |
16 | System.out.printf("%.2f + %.2f + %d = %.2e", floatNum, doubleNum, num, result);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch11/model/ImmutablePoint.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch11.model;
2 |
3 | public class ImmutablePoint {
4 | private final int x;
5 | private final int y;
6 |
7 | public ImmutablePoint() {
8 | x = 0;
9 | y = 0;
10 | }
11 |
12 | public int getX() {
13 | return x;
14 | }
15 |
16 | public int getY() {
17 | return y;
18 | }
19 |
20 | public String pointToString() {
21 | return "(" + x + ", " + y + ")";
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/OverloadingApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | /**
4 | * Overloading Demo.
5 | */
6 | public class OverloadingApp {
7 |
8 | public static void main(String[] args) {
9 |
10 | }
11 |
12 | public static int add(int a, int b) {
13 | return a + b;
14 | }
15 |
16 | public static int add(int a, int b, int c) {
17 | return a + b + c;
18 | }
19 |
20 | public static long add(int a, long b) {
21 | return a + (int) b;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | ISpeakable alice = new Cat();
7 | ISpeakable bob = new Dog();
8 |
9 | alice.speak();
10 | bob.speak();
11 |
12 | doSpeak(alice);
13 | doSpeak(bob);
14 | }
15 |
16 | /*
17 | * Polymorphism
18 | */
19 | public static void doSpeak(ISpeakable iSpeakable) {
20 | iSpeakable.speak();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/IO2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Reads a char using Scanner.
7 | */
8 | public class IO2App {
9 |
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 | char inputChar = ' ';
13 |
14 | System.out.println("Please insert a char");
15 | inputChar = scanner.nextLine().charAt(0);
16 |
17 | System.out.println("Input char: " + inputChar);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/SplitApp_11.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class SplitApp_11 {
4 |
5 | public static void main(String[] args) {
6 | String s = "Athens Uni of Econ and Bus";
7 | String[] tokens = getTokens(s);
8 | for (String token : tokens) {
9 | System.out.print(token + " ");
10 | }
11 | }
12 |
13 | public static String[] getTokens(String s) {
14 | String[] tokens = s.split(" +");
15 | return tokens;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/CompareStr_8.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class CompareStr_8 {
4 |
5 | public static void main(String[] args) {
6 | String s1 = "Athens";
7 | String s2 = "ATHENS";
8 |
9 | if (s1.compareTo(s2) < 0) {
10 | System.out.println("s1 < s2");
11 | } else if (s1.compareTo(s2) > 0) {
12 | System.out.println("s1 > s2");
13 | } else {
14 | System.out.println("s1 == s2");
15 | }
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public class Main {
4 | private final static Runnable joa = new Knight("joa");
5 | private final static Runnable whiteKnight = new Knight("White Knight");
6 | private final static Runnable saintGeorge = new Knight("Saint George");
7 |
8 | public static void main(String[] args) {
9 | (new Thread(joa)).start();
10 | (new Thread(whiteKnight)).start();
11 | (new Thread(saintGeorge)).start();
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/service/exceptions/PhoneNumberAlreadyExistsException.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.service.exceptions;
2 |
3 | import gr.aueb.cf.ch18.model.MobileContact;
4 |
5 | public class PhoneNumberAlreadyExistsException extends Exception {
6 | private final static long serialVersionUID = 123456L;
7 |
8 | public PhoneNumberAlreadyExistsException(MobileContact mobileContact) {
9 | super("Mobile contact with phone number: " + mobileContact.getPhoneNumber() + " already exists");
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/StringHello_1.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class StringHello_1 {
4 |
5 | public static void main(String[] args) {
6 | String s1 = "Hello Coding Factory"; // String literal
7 | String s2 = "Hello Coding Factory";
8 | String s3 = new String("Hello Coding Factory");
9 |
10 | s2 = "Hello Again!";
11 |
12 | System.out.println(s1);
13 | System.out.printf("s2: %s\n", s2);
14 | System.out.printf("s3: %s\n", s3);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/BooleanApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BooleanApp {
6 |
7 | public static void main(String[] args) {
8 | boolean isWoman = false;
9 | Scanner scanner = new Scanner(System.in);
10 |
11 | System.out.println("Are you a woman (true / false)");
12 | isWoman = scanner.nextBoolean();
13 |
14 | System.out.println("Is Woman: " + isWoman);
15 | System.out.printf("Is Woman: %b", isWoman);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/PrintWithSubstring.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Prints a string by using substring()
5 | */
6 | public class PrintWithSubstring {
7 |
8 | public static void main(String[] args) {
9 | String s = "Athens-Uni";
10 |
11 | for (int i = 0; i < s.length(); i++) {
12 | System.out.print(s.substring(i, i+1) + " ");
13 | }
14 |
15 | s = s.replace("-", "");
16 | System.out.println(s);
17 | //System.out.println(s);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/StackApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | public class StackApp {
4 |
5 | public static void main(String[] args) {
6 | MyStack intStack = new MyStack<>();
7 |
8 | intStack.push(1);
9 | intStack.push(2);
10 | intStack.push(3);
11 | intStack.push(4);
12 | intStack.push(5);
13 | intStack.push(6);
14 |
15 | int item = intStack.pop();
16 |
17 | intStack.forEach(System.out::println);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/PopulateApp_1.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | /**
4 | * Populate Demo.
5 | * Populate = insert values to array.
6 | */
7 | public class PopulateApp_1 {
8 |
9 | public static void main(String[] args) {
10 | int[] arr1 = new int[4]; // with new, length is 4
11 | int[] arr2 = {1, 2, 3, 4, 5}; // unsized init, length is 5
12 | int[] arr3 = new int[] {1, 2, 3, 4, 5, 6}; // array initializer, length is 6
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/QueueApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | public class QueueApp {
4 |
5 | public static void main(String[] args) {
6 | MyQueue myQueue = new MyQueue<>();
7 |
8 | myQueue.enQueue("Red Car");
9 | myQueue.enQueue("Green Car");
10 | myQueue.enQueue("Yellow Car");
11 |
12 | String s = myQueue.deQueue();
13 | System.out.println(s + " car was left out of parking");
14 |
15 | myQueue.forEach(System.out::println);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/Cat.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class Cat implements ISpeakable {
4 | private String name;
5 |
6 | public Cat() {
7 |
8 | }
9 |
10 | public Cat(String name) {
11 | this.name = name;
12 | }
13 |
14 | public String getName() {
15 | return name;
16 | }
17 |
18 | public void setName(String name) {
19 | this.name = name;
20 | }
21 |
22 | @Override
23 | public void speak() {
24 | System.out.println(name + " says Hello");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/FactoRecursive.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class FactoRecursive {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 | /**
10 | * Returns n!.
11 | *
12 | * @param n the input number.
13 | * @return the factorial of n.
14 | */
15 | public static int facto(int n) {
16 | /* if (n <= 1) {
17 | return 1;
18 | }
19 |
20 | return n * facto(n-1);*/
21 |
22 | return (n <= 1) ? 1 : n * facto(n-1);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/Dog.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16;
2 |
3 | public class Dog implements ISpeakable {
4 |
5 | private String name;
6 |
7 | public Dog() {
8 |
9 | }
10 |
11 | public Dog(String name) {
12 | this.name = name;
13 | }
14 |
15 | public String getName() {
16 | return name;
17 | }
18 |
19 | public void setName(String name) {
20 | this.name = name;
21 | }
22 |
23 | @Override
24 | public void speak() {
25 | System.out.println(name + "says Hello");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | Book bookNetworks = new Book.Builder(1, "12345").build();
7 |
8 | Book bookJava = new Book.Builder(2, "123456")
9 | .author("Alice W.")
10 | .title("Java in Action")
11 | .build();
12 |
13 | // bookNetworks = new Book.Builder(2, "345678").build();
14 |
15 | System.out.println(bookNetworks);
16 | System.out.println(bookJava);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/PrintStreamApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.PrintStream;
5 |
6 | /**
7 | * Print Stream Demo.
8 | */
9 | public class PrintStreamApp {
10 |
11 | public static void main(String[] args) {
12 | try (PrintStream ps = new PrintStream("C:/tmp/f1.txt")) {
13 | ps.println("Hello Coding Factory");
14 | } catch (FileNotFoundException e) {
15 | // e.printStackTrace();
16 | System.out.println("File not found");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/EqualsStr_7.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class EqualsStr_7 {
4 |
5 | public static void main(String[] args) {
6 | String s1 = "Athens";
7 | String s2 = "Athens";
8 | String s3 = "ATHENS";
9 |
10 | boolean equal1 = s1.equals(s2);
11 | boolean equal2 = s1.equalsIgnoreCase(s3);
12 |
13 | boolean equal3 = s1.toUpperCase().equals(s2.toUpperCase());
14 |
15 | System.out.println(equal1);
16 | System.out.println(equal2);
17 | System.out.println(equal3);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/CompareWithEpsilon.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class CompareWithEpsilon {
4 |
5 | public static void main(String[] args) {
6 | final double EPSILON = 0.000005;
7 | double actual = 0.0;
8 | double expected = 1.0;
9 |
10 | for (int i = 1; i <= 10; i++) {
11 | actual += 0.1;
12 | }
13 |
14 | if (Math.abs(actual - expected) <= EPSILON) {
15 | System.out.println("EQUAL");
16 | } else {
17 | System.out.println("NOT EQUAL");
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/KillTheDragonMission.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public class KillTheDragonMission implements IMission {
4 | private MissionStatus missionStatus = MissionStatus.NOT_STARTED;
5 |
6 | @Override
7 | public MissionStatus getStatus() {
8 | return missionStatus;
9 | }
10 |
11 | @Override
12 | public void setStatus(MissionStatus status) {
13 | this.missionStatus = status;
14 | }
15 |
16 | @Override
17 | public void embark() {
18 | System.out.println("Kill the Dragon");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/SaveThePrincess.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public class SaveThePrincess implements IMission {
4 |
5 | private MissionStatus missionStatus = MissionStatus.NOT_STARTED;
6 |
7 | @Override
8 | public MissionStatus getStatus() {
9 | return missionStatus;
10 | }
11 |
12 | @Override
13 | public void setStatus(MissionStatus status) {
14 | this.missionStatus = status;
15 | }
16 |
17 | @Override
18 | public void embark() {
19 | System.out.println("Save the Princess");
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/MenuApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch1;
2 |
3 | /**
4 | * Εκτυπώνει ένα μενού.
5 | */
6 | public class MenuApp {
7 |
8 | public static void main(String[] args) {
9 | System.out.println("Επιλέξτε μία από τις παρακάτω επιλογές");
10 | System.out.println("1. Εισαγωγή");
11 | System.out.println("2. Διαγραφή");
12 | System.out.println("3. Αναζήτηση");
13 | System.out.println("4. Ενημέρωση");
14 | System.out.println("5. Έξοδος");
15 | System.out.println("Δώστε αριθμό επιλογής:");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/Concat_6.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class Concat_6 {
4 |
5 | public static void main(String[] args) {
6 | String s1 = "Coding";
7 | String s2 = "Factory";
8 |
9 | String s3 = s1 + s2;
10 | String s4 = s1.concat(s2);
11 |
12 | System.out.println("s3: " + s3);
13 | System.out.println("s4: " + s4);
14 |
15 | String s5 = s1 + " " + s2;
16 | String s6 = s1.concat(" ").concat(s2);
17 | System.out.println("s3: " + s5);
18 | System.out.println("s4: " + s6);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch20/regex/PasswordApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch20.regex;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | public class PasswordApp {
7 |
8 | public static void main(String[] args) {
9 | String password = "1axafdz!!!!!aB";
10 |
11 | Pattern pattern = Pattern.compile("(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[!@#$%^&*()-+=])^.{8,}$");
12 | Matcher matcher = pattern.matcher(password);
13 |
14 | while (matcher.find()) {
15 | System.out.println(matcher.group());
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/StringAccessApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Traverses a string and prints the string
5 | * char by char.
6 | */
7 | public class StringAccessApp {
8 |
9 | public static void main(String[] args) {
10 | String s = "Coding Factory";
11 |
12 | for (int i = 0; i < s.length(); i++) {
13 | System.out.print(s.charAt(i) + " ");
14 | }
15 |
16 | System.out.println();
17 |
18 | for (int i = s.length() - 1; i >= 0; i--) {
19 | System.out.print(s.charAt(i) + " ");
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/ImmutablePoint.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | public class ImmutablePoint {
4 | private final int x;
5 | private final int y;
6 |
7 | ImmutablePoint() {
8 | x = 0;
9 | y = 0;
10 | }
11 |
12 | public ImmutablePoint(int x, int y) {
13 | this.x = x;
14 | this.y = y;
15 | }
16 |
17 | public int getX() {
18 | return x;
19 | }
20 |
21 | public int getY() {
22 | return y;
23 | }
24 |
25 | public String convertToString() {
26 | return "(" + x + "," + y + ")";
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/CompareWithoutEpsilon.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class CompareWithoutEpsilon {
4 |
5 | public static void main(String[] args) {
6 | double actual = 0.0;
7 | double expected = 1.0;
8 |
9 | for (int i = 1; i <= 10; i++) {
10 | actual += 0.1;
11 | }
12 |
13 | System.out.printf("Actual: %.20f -- Expected: %.20f\n", actual, expected);
14 |
15 | if (actual == expected) {
16 | System.out.println("EQUAL");
17 | } else {
18 | System.out.println("NOT EQUAL");
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/ArithmeticExceptionApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ArithmeticExceptionApp {
6 |
7 | public static void main(String[] args) {
8 | Scanner in = new Scanner(System.in);
9 | int numerator = 0;
10 | int denominator = 0;
11 | int result;
12 |
13 | System.out.println("Please insert two ints");
14 | numerator = in.nextInt();
15 | denominator = in.nextInt();
16 |
17 | result = numerator / denominator;
18 |
19 | System.out.println("Result: " + result);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/ReadString_2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ReadString_2 {
6 |
7 | public static void main(String[] args) {
8 | Scanner in = new Scanner(System.in);
9 | String s1 = "";
10 | String s2 = "";
11 |
12 | System.out.println("Insert two strings");
13 | s1 = in.next(); // consumes until \n \t whitespaces
14 | s2 = in.nextLine(); // consumes until \n , does not return \n
15 |
16 | System.out.println("Next1: " + s1);
17 | System.out.println("Next2: " + s2);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/StarsDesc10.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | *
5 | */
6 | public class StarsDesc10 {
7 |
8 | public static void main(String[] args) {
9 |
10 | /*for (int i = 1; i <= 10; i++) {
11 | for (int j = 10; j >= i; j--) {
12 | System.out.print("*");
13 | }
14 | System.out.println();
15 | }*/
16 |
17 | for (int i = 10; i >= 1; i--) {
18 | for (int j = 1; j <= i; j++) {
19 | System.out.print("*");
20 | }
21 | System.out.println();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/IndexOfSubstring_4.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | public class IndexOfSubstring_4 {
4 |
5 | public static void main(String[] args) {
6 | String s = "AUEB";
7 | int positionOfA = s.indexOf('A');
8 | int positionOfAU = s.indexOf("U");
9 |
10 | System.out.println("Position of A: " + positionOfA);
11 | System.out.println("Position of A: " + positionOfAU);
12 |
13 | String sub1 = s.substring(1);
14 | System.out.println(sub1);
15 |
16 | String sub2 = s.substring(1, 2);
17 | System.out.println(sub2);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/FactorialApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το n! = 1 * 2 *...* n
7 | * Για παράδειγμα 4! = 1 * 2 * 3 * 4 = 24
8 | */
9 | public class FactorialApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int n = 0;
14 | int facto = 1;
15 | int i = 1;
16 |
17 | System.out.println("Please insert n");
18 | n = scanner.nextInt();
19 |
20 | while (i <= n) {
21 | facto *= i;
22 | i++;
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/IfPositiveApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Ελέγχει αν ένας ακέραιος είναι
7 | * θετικός (>= 0).
8 | */
9 | public class IfPositiveApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int num = 0;
14 |
15 | System.out.println("Please enter an int");
16 | num = scanner.nextInt();
17 |
18 | if (num >= 0) {
19 | System.out.println("Positive");
20 | } else {
21 | System.out.println("Negative");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/SpecialWhileApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | /**
4 | * Ειδικές μορφές της while.
5 | * 0 φορές, 1 φορά, infinite
6 | */
7 | public class SpecialWhileApp {
8 | public static void main(String[] args) {
9 | int i = 1;
10 |
11 | while (i < 0) {
12 | System.out.println("NEVER gets in");
13 | i++;
14 | }
15 |
16 | while (i <= 1) {
17 | System.out.println("Only ONE iteration");
18 | i++;
19 | }
20 |
21 | while (true) {
22 | System.out.println("Eternal while loop");
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/StarsGenericApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Δίνει ο χρήστης (stdin) το πλήθος
7 | * των stars (οριζόντια εκτύπωση).
8 | */
9 | public class StarsGenericApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int stars = 0;
14 | int i = 1;
15 |
16 | System.out.println("Please insert stars count");
17 | stars = scanner.nextInt();
18 |
19 | while (i <= stars) {
20 | System.out.print("*");
21 | i++;
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Array Demo.
5 | */
6 | public class ArrayApp {
7 | public static void main(String[] args) {
8 | int[] arr = new int[3];
9 | arr[0] = 1;
10 | arr[1] = 2;
11 | arr[2] = 3;
12 |
13 | System.out.println("arr[0] = " + arr[0]);
14 | System.out.println("arr[1] = " + arr[1]);
15 | System.out.println("arr[2] = " + arr[2]);
16 |
17 | System.out.printf("arr[0] = %d\n", arr[0]);
18 | System.out.printf("arr[1] = %d\n", arr[1]);
19 | System.out.printf("arr[2] = %d\n", arr[2]);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/MathPowersApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Λαμβάνει ένα ακέραιο από το stdin
7 | * και εκτυπώνει το τετράγωνο και τον
8 | * κύβο.
9 | */
10 | public class MathPowersApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int num = 0;
15 |
16 | System.out.println("Please insert an int");
17 | num = scanner.nextInt();
18 |
19 | System.out.printf("num = %d, num^2 = %d, num^3 = %d",
20 | num, (int) Math.pow(num, 2), (int) Math.pow(num, 3));
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/SwapApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | /**
4 | * Μεταθέτει αμοιβαία τις τιμές δύο
5 | * μεταβλητών.
6 | */
7 | public class SwapApp {
8 |
9 | public static void main(String[] args) {
10 | int a = 10;
11 | int b = 20;
12 | swap(a, b);
13 | System.out.printf("a = %d, b = %d", a, b);
14 | }
15 |
16 | /**
17 | * Swaps two values.
18 | *
19 | * @param a the first value.
20 | * @param b the second value.
21 | */
22 | public static void swap(int a, int b) {
23 | int temp = a;
24 | a = b;
25 | b = temp;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/SymmetricArray_16.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class SymmetricArray_16 {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {1, 2, 3, 2, 1};
7 | System.out.println("Is Symmetric: " + isSymmetric(arr));
8 | }
9 |
10 | public static boolean isSymmetric(int[] arr) {
11 | boolean symmetric = true;
12 |
13 | for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
14 | if (arr[i] != arr[j]) {
15 | symmetric = false;
16 | break;
17 | }
18 | }
19 |
20 | return symmetric;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dao/IMobileContactDAO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dao;
2 |
3 | import gr.aueb.cf.ch18.model.MobileContact;
4 |
5 | import java.util.List;
6 |
7 | public interface IMobileContactDAO {
8 | MobileContact insert(MobileContact mobileContact);
9 | MobileContact update(MobileContact oldMobileContact, MobileContact newMobileContact);
10 | void delete(Long id);
11 | MobileContact get(Long id);
12 | List getAll();
13 |
14 | MobileContact get(String phoneNumber);
15 | boolean phoneNumberExists(String phoneNumber);
16 | boolean userIdExists(Long id);
17 | void delete(String phoneNumber);
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/FactorialApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class FactorialApp {
4 |
5 | public static void main(String[] args) {
6 | int n = 10;
7 | System.out.println("Factorial of " + n + " is: " + FactorialApp.facto(n));
8 | }
9 |
10 | /**
11 | * Returns the factorial.
12 | *
13 | * @param n the input number.
14 | * @return the factorial of n.
15 | */
16 | public static int facto(int n) {
17 | int factorial = 1;
18 |
19 | for (int i = 1; i <= n; i++) {
20 | factorial *= i;
21 | }
22 |
23 | return factorial;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayMinApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Finds the min of an array.
5 | */
6 | public class ArrayMinApp {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {6, 8, 2, 67, 1};
10 |
11 | int minPosition = 0;
12 | int minValue = arr[minPosition];
13 |
14 | for (int i = 1; i < arr.length; i++) {
15 | if (arr[i] < minValue) {
16 | minPosition = i;
17 | minValue = arr[minPosition];
18 | }
19 | }
20 |
21 | System.out.printf("Min value = %d, min position = %d", minValue, minPosition + 1);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/MyQueue.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | import java.util.Deque;
4 | import java.util.LinkedList;
5 | import java.util.function.Consumer;
6 |
7 | /**
8 | * FIFO simulation.
9 | * @param
10 | */
11 | public class MyQueue {
12 | private final Deque myQueue = new LinkedList<>();
13 |
14 | public MyQueue() {
15 |
16 | }
17 |
18 | public void enQueue(T t) {
19 | myQueue.addLast(t);
20 | }
21 |
22 | public T deQueue() {
23 | return myQueue.poll();
24 | }
25 |
26 | public void forEach(Consumer action) {
27 | myQueue.forEach(action);
28 | }
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/InitialsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch1;
2 |
3 | /**
4 | * Εκτυπώνει τα αρχικά του ονόματος
5 | * με αστεράκια.
6 | */
7 | public class InitialsApp {
8 |
9 | public static void main(String[] args) {
10 | System.out.println("********* * ");
11 | System.out.println("* * * *");
12 | System.out.println("* * * *");
13 | System.out.println("********* *******");
14 | System.out.println("* * * *");
15 | System.out.println("* * * *");
16 | System.out.println("********* * *");
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/maps/MapDemo2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.maps;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class MapDemo2 {
7 |
8 | public static void main(String[] args) {
9 |
10 | Map criteria = new HashMap<>() {{
11 | put("price", 10.50);
12 | put("quantity", 10);
13 | put("description", "Oranges");
14 | }};
15 |
16 | Map criteria2 = Map.of("price", 10.30, "quantity", 20, "description", "Oranges");
17 |
18 | Map criteria3 = new HashMap<>(Map.of("price", 10.30, "quantity", 20, "description", "Oranges"));
19 |
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch1/AddApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch1;
2 |
3 | /**
4 | * Προσθέτει δύο ακεραίους και εμφανίζει
5 | * το αποτέλεσμα στην κονσόλα.
6 | */
7 | public class AddApp {
8 | public static void main(String[] args) {
9 |
10 | // Δήλωση και αρχικοποίηση μεταβλητών
11 | int num1 = 10;
12 | int num2 = 20;
13 | int result = 0;
14 |
15 | // Εντολές
16 | result = num1 + num2;
17 |
18 | // Εμφάνιση αποτελεσμάτων
19 | System.out.println("Το αποτέλεσμα των " + num1 + " και " + num2 + " είναι: " + result);
20 | System.out.printf("Το αποτέλεσμα των %d και %d είναι: %d\n", num1, num2, result);
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch11/model/ImmutablePoint3D.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch11.model;
2 |
3 | public class ImmutablePoint3D {
4 | private final Point point; // composition
5 | private final int z;
6 |
7 | public ImmutablePoint3D() {
8 | point = new Point();
9 | z = 0;
10 | }
11 |
12 | public ImmutablePoint3D(Point point, int z) {
13 | this.point = new Point(point.getX(), point.getY()); // deep copy
14 | this.z = z;
15 | }
16 |
17 | public Point getPoint() {
18 | return new Point(point.getX(), point.getY()); // deep copy, defensive copy
19 | }
20 |
21 | public int getZ() {
22 | return z;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch20/regex/NonCapturingGroupsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch20.regex;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | public class NonCapturingGroupsApp {
7 |
8 | public static void main(String[] args) {
9 | String s = "HelloWorld HelloWorldWorld";
10 |
11 | Pattern pattern = Pattern.compile("(Hello)(?:World)+");
12 | Matcher matcher = pattern.matcher(s);
13 |
14 | while (matcher.find()) {
15 | for (int i = 1; i <= matcher.groupCount(); i++) {
16 | System.out.println(matcher.group(i) + " ");
17 | }
18 | System.out.println();
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/MenuApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Εμφανίζει ένα μενού, μέχρι ο
7 | * χρήστης να επιλέξει Quit.
8 | */
9 | public class MenuApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int choice = 0;
14 |
15 | do {
16 | System.out.println("Επιλέξτε ένα από τα παρακάτω:");
17 | System.out.println("1. Εισαγωγή Μαθητή");
18 | System.out.println("2. Διαγραφή Μαθητή");
19 | System.out.println("3. Έξοδος");
20 | choice = scanner.nextInt();
21 | } while (choice != 3);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/MulGenApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το 1 * 2 * ... * n,
7 | * όπου το n το δίνει ο χρήστης.
8 | */
9 | public class MulGenApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int endValue = 0;
14 | int i = 1;
15 | int result = 1;
16 |
17 | System.out.println("Please insert a num (int)");
18 | endValue = scanner.nextInt();
19 |
20 | while (i <= endValue) {
21 | result *= i;
22 | i++;
23 | }
24 | System.out.printf("Result: %,d ", result);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForPowerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το base^power
7 | */
8 | public class ForPowerApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 | int base = 0;
13 | int power = 0;
14 | int result = 1;
15 |
16 | System.out.println("Please insert base, power");
17 | base = scanner.nextInt();
18 | power = scanner.nextInt();
19 |
20 | for (int i = 1; i <= power; i++) {
21 | result *= base;
22 | }
23 |
24 | System.out.printf("%d^%d = %,d", base, power, result);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch5/readme.md:
--------------------------------------------------------------------------------
1 | Δημιουργήστε μία εφαρμογή που να εμφανίζει ένα μενού με 6 επιλογές:
2 | 1. Εμφάνισε n αστεράκια οριζόντια
3 | 2. Εμφάνισε n αστεράκια κάθετα
4 | 3. Εμφάνισε n γραμμές με n αστεράκια
5 | 4. Εμφάνισε n γραμμές με αστεράκια 1 – n
6 | 5. Εμφάνισε n γραμμές με αστεράκια n – 1
7 | 6. Έξοδος από το πρόγραμμα
8 | Δώσε επιλογή:
9 |
10 | Και μετά: Δώστε αριθμό stars.
11 |
12 | Κάθε μία από τις πέντε πρώτες επιλογές θα πρέπει να υλοποιηθεί με μέθοδο
13 | 1. Ο χρήστης θα δίνει ένα αριθμό για αστεράκια
14 | 2. Οι επιλογές 3, 4, και 5 θα πρέπει να υλοποιηθούν με τη βοήθεια της μεθόδου της επιλογής 1
15 | 3. Το πρόγραμμα θα πρέπει να τρέχει μέχρι ο χρήστης να επιλέξει την Επιλογή 6
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/SumGenApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το 1 + 2 + .. + n,
7 | * όπου το n το δίνει ο χρήστης.
8 | */
9 | public class SumGenApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int endValue = 0;
14 | int i = 1;
15 | int result = 0;
16 |
17 | System.out.println("Please insert a num (int)");
18 | endValue = scanner.nextInt();
19 |
20 | while (i <= endValue) {
21 | result += i;
22 | i++;
23 | }
24 | System.out.printf("Sum 1+2+..+%d = %d", endValue, result);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/TemperatureApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Αν η θερμοκρασία είναι < 0 τότε
7 | * η isTempBelowZero γίνεται true,
8 | * αλλιώς γίνεται false.
9 | */
10 | public class TemperatureApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int temperature = 0;
15 | boolean isTempBelowZero = false;
16 |
17 | System.out.println("Please insert temperature");
18 | temperature = scanner.nextInt();
19 |
20 | isTempBelowZero = (temperature < 0);
21 |
22 | System.out.println("Temp is below zero: " + isTempBelowZero);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/BigDecimalApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | import java.math.BigDecimal;
4 |
5 | public class BigDecimalApp {
6 |
7 | public static void main(String[] args) {
8 | BigDecimal actual = new BigDecimal("0.0");
9 | BigDecimal expected = new BigDecimal("1.0");
10 |
11 | for (int i = 1; i <= 10; i++) {
12 | actual = actual.add(BigDecimal.valueOf(0.1));
13 | }
14 |
15 | System.out.println(actual);
16 | System.out.println(expected);
17 |
18 | if (actual.equals(expected)) {
19 | System.out.println("EQUAL");
20 | } else {
21 | System.out.println("NOT EQUAL");
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/BubbleSort.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Sorts an array with
5 | */
6 | public class BubbleSort {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {6, 9, 4, 2, 12};
10 |
11 | for (int i = arr.length - 1; i > 0; i--) {
12 | for (int j = 0; j < i; j++) {
13 | if (arr[j] > arr[j+1]) swap(arr, j, j+1);
14 | }
15 | }
16 |
17 | for (int num : arr) {
18 | System.out.print(num + " ");
19 | }
20 | }
21 |
22 | public static void swap(int[] arr, int i, int j) {
23 | int tmp = arr[i];
24 | arr[i] = arr[j];
25 | arr[j] = tmp;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/StringSearchApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Searches for chars or substrings.
5 | */
6 | public class StringSearchApp {
7 |
8 | public static void main(String[] args) {
9 | String s = "Athens Uni of Eco Uni and Bus";
10 |
11 | if (s.contains("Uni")) {
12 | System.out.println("Bingo");
13 | }
14 |
15 | char ch = 'a';
16 | int index = s.indexOf(ch);
17 | System.out.println(index);
18 |
19 | if (s.startsWith("Athens")) {
20 | System.out.println("Starts with Athens");
21 | }
22 |
23 | String athens = s.substring(6);
24 | System.out.println(athens);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/Swap_4.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | /**
4 | * Swap Demo.
5 | */
6 | public class Swap_4 {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {1, 2};
10 |
11 | System.out.printf("a=%d, b=%d\n", arr[0], arr[1]);
12 | swap(arr);
13 | System.out.printf("a=%d, b=%d", arr[0], arr[1]);
14 | }
15 |
16 | /**
17 | * Swaps the elements of an array.
18 | * @param arr
19 | */
20 | public static void swap(int[] arr) {
21 | if (arr == null || arr.length != 2) {
22 | return;
23 | }
24 |
25 | int tmp = arr[0];
26 | arr[0] = arr[1];
27 | arr[1] = tmp;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch3/readme.md:
--------------------------------------------------------------------------------
1 | **Άσκηση 1**
2 |
3 | Θέλουμε να αναπτύξουμε ένα πρόγραμμα που να αποφαίνεται αν ένα έτος είναι δίσεκτο ή όχι. Δηλαδή να προτρέπει τον χρήστη να
4 | δώσει ένα έτος από το πληκτρολόγιο, να διαβάζει με Scanner το έτος (ακέραιος), να κάνει την επεξεργασία και να εμφανίζει στην οθόνη αν το έτος αυτό είναι δίσεκτο ή όχι.
5 |
6 | Δίσεκτο είναι ένα έτος αν έχει 366 ημέρες, αντί 365. Πότε όμως ένα έτος έχει 366 ημέρες;
7 |
8 | • Αν διαιρείται με το 4 ΕΚΤΟΣ ΕΑΝ
9 | – Διαιρείται ακριβώς και με το 100
10 | – Αλλά όχι με το 400
11 |
12 | • Άλλος τρόπος να το εκφράσουμε πιο απλά είναι: ένα έτος είναι δίσεκτο αν (διαιρείται ακριβώς με το 4 και όχι με το 100) ή αν (διαιρείται ακριβώς με το 400)
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/Point.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | import java.util.Random;
4 |
5 | public class Point {
6 | private int x;
7 | private int y;
8 |
9 | public Point() {
10 | }
11 |
12 | public Point(int x, int y) {
13 | this.x = x;
14 | this.y = y;
15 | }
16 |
17 | /**
18 | * Static factory method.
19 | *
20 | * @return a {@link Point} instance.
21 | */
22 | public static Point getZeroPoint() {
23 | return new Point();
24 | }
25 |
26 | public static Point getRandomPoint() {
27 | Random rnd = new Random(System.currentTimeMillis());
28 | return new Point(rnd.nextInt(101), rnd.nextInt(101));
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/marker/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.marker;
2 |
3 | public class Main {
4 |
5 | public static void main(String[] args) {
6 | Book book = new Book(1, "123", "Stallings W.", "TCP/IP Networks");
7 | Item cd = new CompactDisk(1, "Apples and Oranges");
8 |
9 | deliver(book);
10 | deliver(cd);
11 | }
12 |
13 | public static void deliver(Item item) {
14 | if (item instanceof Book) {
15 | System.out.println("Book was delivered");
16 | } else if (item instanceof CompactDisk) {
17 | System.out.println("CD was delivered");
18 | } else {
19 | throw new RuntimeException();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/EvenOrOddApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class EvenOrOddApp {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 | /**
10 | * Evaluates a number if is even.
11 | *
12 | * @param a the input number.
13 | * @return true, if is even, false otherwise.
14 | */
15 | public static boolean isEven(int a) {
16 | return (a % 2) == 0;
17 | }
18 |
19 | /**
20 | * Evaluates a number if is odd.
21 | *
22 | * @param a the input number.
23 | * @return true, if is odd, false otherwise.
24 | */
25 | public static boolean isOdd(int a) {
26 | return !isEven(a);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/MappingApp_7.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class MappingApp_7 {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {1, 2, 3, 4, 5};
7 | int[] mappedArr = mapDouble(arr);
8 | printArr(mappedArr);
9 | }
10 |
11 | public static int[] mapDouble(int[] arr) {
12 | int[] returnedArr = new int[arr.length];
13 |
14 | for (int i = 0; i < arr.length; i++) {
15 | returnedArr[i] = arr[i] * 2;
16 | }
17 | return returnedArr;
18 | }
19 |
20 | public static void printArr(int[] arr) {
21 | for (int item : arr) {
22 | System.out.print(item + " ");
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArraysUtilCopy.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | import java.util.Arrays;
4 |
5 | /**
6 | * Demonstrates the Arrays utility class.
7 | */
8 | public class ArraysUtilCopy {
9 |
10 | public static void main(String[] args) {
11 | int[] arr = {1, 2, 3, 4, 5};
12 |
13 | int[] arrCopy = Arrays.copyOf(arr, arr.length);
14 | printArray(arrCopy);
15 | System.out.println();
16 |
17 | int[] arrCopy2 = Arrays.copyOfRange(arr, 1, arr.length);
18 | printArray(arrCopy2);
19 |
20 | }
21 |
22 | public static void printArray(int[] arr) {
23 | for (int el : arr) {
24 | System.out.print(el + " ");
25 | }
26 | }
27 |
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/EmojisApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Εμφανίζει όλα τα emojis στο range
5 | * U+1F600 - U+1F64F
6 | */
7 | public class EmojisApp {
8 |
9 | public static void main(String[] args) {
10 | int emojiStart = 0x1F600;
11 | int emojiEnd = 0x1F64F;
12 | int counter = 0;
13 | int emoji;
14 |
15 | emoji = emojiStart;
16 | while (emoji <= emojiEnd) {
17 | System.out.print(Character.toChars(emoji)); // surrogate pairs
18 | System.out.print(" ");
19 | emoji++;
20 | counter++;
21 | if (counter % 16 == 0) {
22 | System.out.println();
23 | }
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/IOApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Demonstrates I/O with Strings.
7 | */
8 | public class IOApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner in = new Scanner(System.in);
12 | String s; // null
13 | int num;
14 | System.out.println("Please insert a string that ends with whitespace (return/tab/spaces)");
15 | s = in.next();
16 | in.nextLine();
17 | //num = Integer.parseInt(s);
18 | System.out.println(s);
19 |
20 | // System.out.println("Please insert a string that ends with return");
21 | // s = in.nextLine();
22 | // System.out.print(s);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/DigitCountApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Μετράει το πλήθος των ψηφίων ενός ακεραίου
7 | * που δίνει ο χρήστης.
8 | */
9 | public class DigitCountApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int inputNum = 0;
14 | int num = 0;
15 | int count = 0;
16 |
17 | System.out.println("Please insert num");
18 | inputNum = scanner.nextInt();
19 |
20 | num = inputNum;
21 | do {
22 | count++;
23 | num = num / 10;
24 | } while (num != 0);
25 |
26 | System.out.println("Digit Count: " + count);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/SwapApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Swap with arrays.
5 | */
6 | public class SwapApp {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {1, 2};
10 | print(arr);
11 | System.out.println();
12 |
13 | swap(arr);
14 | print(arr);
15 | }
16 |
17 | public static void swap(int[] arr) {
18 | if (arr == null) return;
19 | if (arr.length != 2) return;
20 |
21 | int tmp = arr[0];
22 | arr[0] = arr[1];
23 | arr[1] = tmp;
24 | }
25 |
26 | public static void print(int[] arr) {
27 | for (int element : arr) {
28 | System.out.print(element + " ");
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/ReducingApp_11.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class ReducingApp_11 {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 | public static int getSum(int[] arr) {
10 | if (arr == null || arr.length <= 0) return 0;
11 |
12 | int sum = 0;
13 |
14 | for (int item : arr) {
15 | sum += item;
16 | }
17 | return sum;
18 | }
19 |
20 | public static int getAvg(int[] arr) {
21 | if (arr == null || arr.length <= 0) return 0;
22 |
23 | int sum = 0;
24 | int avg = 0;
25 |
26 | for (int item : arr) {
27 | sum += item;
28 | }
29 | return sum / arr.length;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Bingo2IfApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Bingo2IfApp {
6 |
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 | int inputNum = 0;
10 | final int SECRET = 12;
11 |
12 | while (true) {
13 | System.out.println("Please insert a num");
14 | inputNum = scanner.nextInt();
15 |
16 | if (inputNum == SECRET) {
17 | System.out.println("Bingo!");
18 | break;
19 | }
20 |
21 | System.out.println("Not found. Try Again!");
22 |
23 | }
24 | System.out.println("Thanks for using Bingo Game");
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch20/regex/GreedyApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch20.regex;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | public class GreedyApp {
7 |
8 |
9 | public static void main(String[] args) {
10 | String passwd = "login:thanos;pass:123456;";
11 |
12 | // Greedy match
13 | Pattern patternGreedy = Pattern.compile(".*;");
14 |
15 | // Reluctant
16 | Pattern patternReluctant = Pattern.compile(".*?;");
17 |
18 | //Matcher matcher = patternGreedy.matcher(passwd);
19 | Matcher matcher = patternReluctant.matcher(passwd);
20 |
21 | while (matcher.find()) {
22 | System.out.println(matcher.group(0));
23 | }
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/PowerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το a^n με while. Π.χ.
7 | * Το 2^10 = 1024, το 3^3 = 27
8 | */
9 | public class PowerApp {
10 |
11 | public static void main(String[] args) {
12 | int base = 0;
13 | int power = 0;
14 | int result = 1;
15 | int i = 1;
16 | Scanner scanner = new Scanner(System.in);
17 |
18 | System.out.println("Please insert base and power");
19 | base = scanner.nextInt();
20 | power = scanner.nextInt();
21 |
22 | while (i <= power) {
23 | result *= base;
24 | i++;
25 | }
26 | System.out.println("Result: " + result);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch2/FahrenheitToCelsiusApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Μετατρέπει βαθμούς Fahrenheit σε βαθμούς
7 | * Κελσίου σε τιμές που δίνει ο χρήστης από
8 | * την κονσόλα (stdin).
9 | */
10 | public class FahrenheitToCelsiusApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int fahrenheit = 0;
15 | int celsius = 0;
16 |
17 | System.out.print("Please insert degrees Fahrenheit: ");
18 | fahrenheit = scanner.nextInt();
19 |
20 | celsius = 5 * (fahrenheit - 32) / 9;
21 |
22 | System.out.printf("%d\u2109 = %d\u2103\n", fahrenheit, celsius);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/MyStack.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | import java.util.ArrayDeque;
4 | import java.util.Deque;
5 | import java.util.function.Consumer;
6 |
7 | /**
8 | * Simulates a LIFO data structure.
9 | * @param
10 | */
11 | public class MyStack {
12 | private final Deque myStack = new ArrayDeque<>();
13 |
14 | public MyStack() {
15 | }
16 |
17 | public void push(T t) {
18 | myStack.push(t);
19 | }
20 |
21 | public T pop() {
22 | return myStack.pop();
23 | }
24 |
25 | public boolean isEmpty() {
26 | return myStack.isEmpty();
27 | }
28 |
29 | public void forEach(Consumer action) {
30 | myStack.forEach(action);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/ScannerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Scanner Demo. Reads two ints from
7 | * stdin and prints the sum.
8 | */
9 | public class ScannerApp {
10 | public static void main(String[] args) {
11 |
12 | // Δήλωση και αρχικοποίηση μεταβλητών
13 | Scanner in = new Scanner(System.in);
14 | int num1;
15 | int num2;
16 | int sum;
17 |
18 | // Εντολές
19 | System.out.println("Please insert two ints");
20 | num1 = in.nextInt();
21 | num2 = in.nextInt();
22 |
23 | sum = num1 * num2;
24 |
25 | // Εκτύπωση αποτελεσμάτων
26 | System.out.printf("Sum of %d + %d = %d", num1, num2, sum);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForBigIntApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.math.BigInteger;
4 | import java.util.Scanner;
5 |
6 | /**
7 | * Χρησιμοποιεί την κλάση BigInteger.
8 | */
9 | public class ForBigIntApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner in = new Scanner(System.in);
13 | int a = 0;
14 | int n = 0;
15 | BigInteger result = BigInteger.valueOf(1);
16 |
17 | System.out.println("Please insert base, power");
18 | a = in.nextInt();
19 | n = in.nextInt();
20 |
21 | for (int i = 1; i <= n; i++) {
22 | result = result.multiply(BigInteger.valueOf(a));
23 | }
24 |
25 | System.out.println("Result: " + result);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/clone/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.clone;
2 |
3 | import java.io.*;
4 |
5 | public class Main {
6 |
7 | public static void main(String[] args) throws CloneNotSupportedException {
8 | Trainee alice = new Trainee("Alice", new City("Athens"));
9 | Trainee bob = alice.clone();
10 | bob.getCity().setDescription("London");
11 | System.out.println(alice);
12 |
13 |
14 | // try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:/tmp/trainee.ser"))) {
15 | // Trainee alice = new Trainee("Alice", new City("Athens"));
16 | // oos.writeObject(alice);
17 | // } catch (IOException e) {
18 | // e.printStackTrace();
19 | // }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/SumApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch1;
2 |
3 | /**
4 | * Πρώτον: Δηλώνει δύο μεταβλητές ακεραίων num1 και num2
5 | * και τις αρχικοποιεί στις τιμές 19 και 30 αντίστοιχα.
6 | * Δεύτερον: Υπολογίζει το άθροισμα num1 + num2 και το
7 | * αποθηκεύει στη μεταβλητή sum. Και, Τρίτον: εκτυπώνει
8 | * το εξής κείμενο: Το αποτέλεσμα της πρόσθεσης είναι
9 | * ίσο με [το αποτέλεσμα της πρόσθεσης]. Αναμενόμενη τιμή
10 | * είναι το 49.
11 | */
12 | public class SumApp {
13 |
14 | public static void main(String[] args) {
15 | int num1 = 19;
16 | int num2 = 30;
17 | int sum = 0;
18 |
19 | sum = num1 + num2;
20 |
21 | System.out.println("Το αποτέλεσμα της πρόσθεσης είναι ίσο με " + sum);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch8/readme.md:
--------------------------------------------------------------------------------
1 | **Exceptions**
2 |
3 | Αναπτύξτε μία μέθοδο που εμφανίζει ένα μενού με επιλογές από 1 έως 4 (και 5 για Exit) καθώς και μία μέθοδο int getChoice() για να διαβάσετε το choice του χρήστη με Scanner
4 |
5 | Στη μέθοδο int getChoice() αν ο χρήστης δώσει μη-ακέραιο ελέγξτε κατάλληλα με state-testing (hasNextInt())
6 |
7 | Αναπτύξτε επίσης μία μέθοδο void printChoice(int choice) που εκτυπώνει την επιλογή. Αν η επιλογή δεν είναι μεταξύ 1-5 τότε δημιουργεί (throws) ένα IllegalArgumentException, το οποίο παρότι είναι RunTimeException, θα μπορούσαμε στη συγκεκριμένη περίπτωση να κάνουμε recover και να κάναμε try/catch στη main
8 |
9 | Διαμορφώστε τη main ώστε να κάνει catch το exception και να επικοινωνεί με τον χρήστη με κατάλληλο μήνυμα
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/products/Milk.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.products;
2 |
3 | public class Milk extends AbstractGenericProduct implements IMilk {
4 | private String manufacturer;
5 |
6 | public Milk() {
7 |
8 | }
9 |
10 | public Milk(long id, String productName, String description, double price, int quantity, String manufacturer) {
11 | super(id, productName, description, price, quantity);
12 | this.manufacturer = manufacturer;
13 | }
14 |
15 | public String getManufacturer() {
16 | return manufacturer;
17 | }
18 |
19 | public void setManufacturer(String manufacturer) {
20 | this.manufacturer = manufacturer;
21 | }
22 |
23 | @Override
24 | public Milk get() {
25 | return this;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/PrintStreamAutoFlushApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.FileOutputStream;
5 | import java.io.PrintStream;
6 | import java.nio.charset.StandardCharsets;
7 |
8 | /**
9 | * FileStream with auto-flush.
10 | * FileOutputStream with true = append
11 | */
12 | public class PrintStreamAutoFlushApp {
13 |
14 | public static void main(String[] args) {
15 | try (PrintStream ps = new PrintStream(new FileOutputStream("C:/tmp/f5.txt", true), true, StandardCharsets.UTF_8)) {
16 | ps.println("Hello Coding Factory");
17 | } catch (FileNotFoundException e) {
18 | // e.printStackTrace();
19 | System.out.println("File not found");
20 | }
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/IfAbsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Διαβάζει ένα αριθμό από τον χρήστη
7 | * και επιστρέφει το απόλυτο του αριθμού.
8 | */
9 | public class IfAbsApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int num = 0;
14 | int abs = 0;
15 |
16 | System.out.println("Please insert a num");
17 | num = scanner.nextInt();
18 |
19 | // Ternary Operator - Τριαδικός τελεστής
20 | abs = (num >= 0) ? num : -num;
21 |
22 | /* if (num >= 0) {
23 | abs = num;
24 | } else {
25 | abs = -num;
26 | }*/
27 |
28 | System.out.println("Abs of: " + num + " is: " + abs);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayCopy.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Demonstrates array copy.
5 | */
6 | public class ArrayCopy {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {1, 2};
10 | //int[] clone = shallowCopy(arr);
11 | int[] clone = deepCopy(arr);
12 | clone[0] = 500;
13 |
14 | for (int el : arr) {
15 | System.out.print(el + " ");
16 | }
17 | }
18 |
19 | public static int[] shallowCopy(int[] arr) {
20 | return arr;
21 | }
22 |
23 | public static int[] deepCopy(int[] arr) {
24 | int[] arrayToReturn = new int[arr.length];
25 | for (int i = 0; i < arr.length; i++) {
26 | arrayToReturn[i] = arr[i];
27 | }
28 | return arrayToReturn;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/BufferedReaderApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.FileReader;
5 | import java.io.IOException;
6 |
7 | /**
8 | * Demo of Buffered reader in combination
9 | * with StringBuilder.
10 | */
11 | public class BufferedReaderApp {
12 |
13 | public static void main(String[] args) {
14 | StringBuilder sb = new StringBuilder();
15 |
16 | try (BufferedReader bf = new BufferedReader(new FileReader("C:/tmp/f-reader.txt"))) {
17 | String line = "";
18 |
19 | while ((line = bf.readLine()) != null) {
20 | sb.append(line).append("\n");
21 | }
22 | } catch (IOException e) {
23 | e.printStackTrace();
24 | }
25 | System.out.println(sb);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dto/UserDetailsInsertDTO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dto;
2 |
3 | public class UserDetailsInsertDTO extends BaseDTO {
4 | private String firstname;
5 | private String lastname;
6 |
7 | public UserDetailsInsertDTO() {
8 |
9 | }
10 |
11 | public UserDetailsInsertDTO(String firstname, String lastname) {
12 | this.firstname = firstname;
13 | this.lastname = lastname;
14 | }
15 |
16 | public String getFirstname() {
17 | return firstname;
18 | }
19 |
20 | public void setFirstname(String firstname) {
21 | this.firstname = firstname;
22 | }
23 |
24 | public String getLastname() {
25 | return lastname;
26 | }
27 |
28 | public void setLastname(String lastname) {
29 | this.lastname = lastname;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dto/UserDetailsUpdateDTO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dto;
2 |
3 | public class UserDetailsUpdateDTO extends BaseDTO {
4 | private String firstname;
5 | private String lastname;
6 |
7 | public UserDetailsUpdateDTO() {
8 |
9 | }
10 |
11 | public UserDetailsUpdateDTO(String firstname, String lastname) {
12 | this.firstname = firstname;
13 | this.lastname = lastname;
14 | }
15 |
16 | public String getFirstname() {
17 | return firstname;
18 | }
19 |
20 | public void setFirstname(String firstname) {
21 | this.firstname = firstname;
22 | }
23 |
24 | public String getLastname() {
25 | return lastname;
26 | }
27 |
28 | public void setLastname(String lastname) {
29 | this.lastname = lastname;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/ExprApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | /**
4 | * Demonstrates arithmetic operators.
5 | */
6 | public class ExprApp {
7 | public static void main(String[] args) {
8 | int num1 = 12;
9 | int num2 = 20;
10 | int sum = 0;
11 | int result1 = 0;
12 |
13 | sum = num1 + num2; // =32
14 |
15 | //result1 = sum++; // result=32, sum=33
16 | //result1 = ++sum; // result = 33, sum = 33
17 |
18 | // sum++; // 33
19 | sum = sum + 1;
20 |
21 | sum--;
22 | sum = sum - 1;
23 |
24 | result1 = sum--; // 32
25 |
26 | result1 = result1 + 10;
27 | result1 += 10;
28 | result1 -= 20;
29 | result1 *= 2;
30 | result1 /= 4;
31 | result1 %= 5;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/PositiveCountApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Όσο ο χρήστης δίνει ακεραίους
7 | * τους μετράμε μέχρι ο χρήστης να δώσει
8 | * -1 (sentinel value).
9 | */
10 | public class PositiveCountApp {
11 |
12 | public static void main(String[] args) {
13 | int inputNum = 0;
14 | int count = 0;
15 | Scanner scanner = new Scanner(System.in);
16 |
17 | System.out.println("Please insert a num (-1 for Quit)");
18 | inputNum = scanner.nextInt();
19 |
20 | while (inputNum != -1) {
21 | count++;
22 | System.out.println("Please insert a num (-1 for Quit)");
23 | inputNum = scanner.nextInt();
24 | }
25 |
26 | System.out.println("Count: " + count);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/AddApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Διαβάζει δύο ακεραίους από το stdin
7 | * Προσθέτει τους ακεραίους
8 | * Εκτυπώνει το αποτέλεσμα στην κονσόλα.
9 | */
10 | public class AddApp {
11 |
12 | public static void main(String[] args) {
13 |
14 | // Δήλωση και αρχικοποίηση μεταβλητών
15 | Scanner scanner = new Scanner(System.in);
16 | int num1 = 0;
17 | int num2 = 0;
18 | int result = 0;
19 |
20 | // Εντολές
21 | System.out.println("Please insert two ints");
22 | num1 = scanner.nextInt();
23 | num2 = scanner.nextInt();
24 | result = num1 + num2;
25 |
26 | // Εκτύπωση αποτελεσμάτων
27 | System.out.printf("%,d + %,d = %,d", num1, num2, result);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/TryWithResourcesApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.util.Scanner;
6 |
7 | public class TryWithResourcesApp {
8 |
9 | public static void main(String[] args) throws FileNotFoundException {
10 | int num = 0;
11 | File fd = new File("C:\\tmp\\hello.txt");
12 |
13 | try (Scanner in = new Scanner(fd)) {
14 | if (in.hasNextInt()) {
15 | num = in.nextInt();
16 | System.out.println(num);
17 | } else {
18 | System.out.println("Error in input");
19 | in.nextLine();
20 | }
21 | } catch (FileNotFoundException e) {
22 | e.printStackTrace();
23 | throw e;
24 | }
25 | }
26 | }
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/HelloChar.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | /**
4 | * Demonstration. Declare, init chars.
5 | */
6 | public class HelloChar {
7 |
8 | public static void main(String[] args) {
9 | char star = '*';
10 | char alpha = 'a';
11 | char upperAlpha = 'A';
12 | char dot = '.';
13 | char bang = '!';
14 | final char EMPTY_SPACE = ' ';
15 | int hash = '#';
16 |
17 | System.out.print((int) star);
18 | System.out.print(alpha);
19 | System.out.println(dot);
20 |
21 | System.out.println("Hash: " + (char) hash);
22 |
23 | if (alpha > upperAlpha) {
24 | System.out.println("a is greater (greater ordinal) than A");
25 | } else {
26 | System.out.println("A is greater than a");
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/AddApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | public class AddApp {
4 |
5 | public static void main(String[] args) {
6 | int a = 10;
7 | int b = 20;
8 | int result = 0;
9 |
10 | result = AddApp.add(a, b);
11 |
12 | System.out.printf("%d + %d = %d", a, b, result);
13 | }
14 |
15 | /**
16 | * Returns the sum of two integers.
17 | *
18 | * @param a the first integer.
19 | * @param b the second integer.
20 | * @return the sum of a, b.
21 | */
22 | public static int add(int a, int b) {
23 | return a + b;
24 |
25 |
26 | /* // Δήλωση και αρχικοποίηση
27 | int sum = 0;
28 |
29 | // Εντολές
30 | sum = a + b;
31 |
32 | // Επιστροφή αποτελέσματος
33 | return sum;*/
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/ForFlexibleApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ForFlexibleApp {
6 |
7 | public static void main(String[] args) {
8 | Scanner scanner = new Scanner(System.in);
9 | int startValue = 0;
10 | int endValue = 0;
11 | int step = 0;
12 | int iterations = 0;
13 |
14 | System.out.println("Please insert start value, end value, step");
15 | startValue = scanner.nextInt();
16 | endValue = scanner.nextInt();
17 | step = scanner.nextInt();
18 |
19 | for (int i = startValue; i <= endValue; i = i + step) {
20 | iterations++;
21 | System.out.print(i + " ");
22 | }
23 |
24 | System.out.println();
25 | System.out.println("Iterations: " + iterations);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/products/model/Honey.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.products.model;
2 |
3 | public class Honey extends Product {
4 | private boolean isAvailable;
5 |
6 | public Honey() {
7 |
8 | }
9 |
10 | public Honey(long id, String productName, String description, double price, int quantity, boolean isAvailable) {
11 | super(id, productName, description, price, quantity);
12 | this.isAvailable = isAvailable;
13 | }
14 |
15 | public boolean isAvailable() {
16 | return isAvailable;
17 | }
18 |
19 | public void setAvailable(boolean available) {
20 | isAvailable = available;
21 | }
22 |
23 | @Override
24 | public String toString() {
25 | return super.toString() + " Honey{" +
26 | "isAvailable=" + isAvailable +
27 | '}';
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/service/exceptions/ContactNotFoundException.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.service.exceptions;
2 |
3 | import gr.aueb.cf.ch18.model.MobileContact;
4 |
5 | public class ContactNotFoundException extends Exception {
6 | private static final long serialVersionUID = 67235093L;
7 |
8 | public ContactNotFoundException(String phoneNumber) {
9 | super("The mobile contact with phone number: " + phoneNumber + " not found");
10 | }
11 |
12 | public ContactNotFoundException(Long id) {
13 | super("The mobile contact with id: " + id + " not found");
14 | }
15 |
16 | public ContactNotFoundException(MobileContact mobileContact) {
17 | super("The mobile contact with id: " + mobileContact.getId()
18 | + " and phone number: " + mobileContact.getPhoneNumber() + " not found");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/IntApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | /**
4 | * Demonstrates int data types.
5 | */
6 | public class IntApp {
7 |
8 | public static void main(String[] args) {
9 | System.out.printf("Type: %s\tSize: %2d bits\tMin: %,d, Max: %,d\n",
10 | Integer.TYPE, Integer.BYTES * 8, Integer.MIN_VALUE, Integer.MAX_VALUE);
11 | System.out.printf("Type: %s\tSize: %2d bits\tMin: %d, Max: %d\n",
12 | Byte.TYPE, Byte.BYTES * 8, Byte.MIN_VALUE, Byte.MAX_VALUE);
13 | System.out.printf("Type: %s\tSize: %2d bits\tMin: %,d, Max: %,d\n",
14 | Short.TYPE, Short.BYTES * 8, Short.MIN_VALUE, Short.MAX_VALUE);
15 | System.out.printf("Type: %s\tSize: %2d bits\tMin: %,d, Max: %,d\n",
16 | Long.TYPE, Long.BYTES * 8, Long.MIN_VALUE, Long.MAX_VALUE);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/IfGradesApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Ελέγχει ένα βαθμό και αντίστοιχα
7 | * δίνει output (Excellent, Very Good, Good, Fail)
8 | */
9 | public class IfGradesApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int grade = 0;
14 |
15 | System.out.println("Please insert grade");
16 | grade = scanner.nextInt();
17 |
18 | if (grade >= 8) { // <= 10
19 | System.out.println("Excellent");
20 | } else if (grade >= 6) {
21 | System.out.println("Very Good");
22 | } else if (grade >= 4) {
23 | System.out.println("Pass");
24 | } else { // >= 0 && <= 3
25 | System.out.println("Fail");
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/SnowingApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Διαβάζει μία θερμοκρασία και ένα boolean
7 | * για το αν βρέχει, από το stdin και
8 | * αποφασίζει εάν χιονίζει.
9 | */
10 | public class SnowingApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int temp = 0;
15 | boolean isRaining = false;
16 | boolean isSnowing = false;
17 |
18 | System.out.println("Please insert temp");
19 | temp = scanner.nextInt();
20 | System.out.println("Please insert if is raining (true / false)");
21 | isRaining = scanner.nextBoolean();
22 |
23 | isSnowing = isRaining && temp < 0; // short-circuit
24 |
25 | System.out.println("Is snowing: " + isSnowing);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArraySearchApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Demonstrates searching for an
5 | * element of an array with specific
6 | * value.
7 | */
8 | public class ArraySearchApp {
9 |
10 | public static void main(String[] args) {
11 | int[] arr = {1, 2, 3, 4, 5};
12 | final int SECRET = 5;
13 | boolean secretWasFound = false;
14 |
15 | for (int num : arr) {
16 | if (num == SECRET) {
17 | secretWasFound = true;
18 | break;
19 | }
20 | }
21 |
22 | /*if (secretWasFound) {
23 | System.out.println("Secret was found");
24 | } else {
25 | System.out.println("Secret was not found");
26 | }*/
27 |
28 | System.out.printf("Secret was%sfound", (secretWasFound) ? " " : " not ");
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/FlushApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.PrintStream;
5 | import java.io.PrintWriter;
6 | import java.util.Scanner;
7 |
8 | /**
9 | * Flushes the contents of print stream.
10 | */
11 | public class FlushApp {
12 |
13 | public static void main(String[] args) {
14 | try (PrintWriter pw = new PrintWriter("C:/tmp/f2.txt");
15 | Scanner in = new Scanner(System.in)) {
16 | pw.print("Hello Coding Factory");
17 | pw.flush();
18 | System.out.println("Please insert");
19 | in.nextLine();
20 | // pw.close();
21 | // in.close();
22 | } catch (FileNotFoundException e) {
23 | // e.printStackTrace();
24 | System.out.println("File not found");
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch11/StudentApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch11;
2 |
3 | /**
4 | * Driver class.
5 | * Creates an instance/object of Student class
6 | */
7 | public class StudentApp {
8 |
9 | public static void main(String[] args) {
10 | Student alice = new Student();
11 | Student bob = new Student();
12 | Student chris = new Student();
13 |
14 | Student mark = new Student(4, "Mark", "Z.");
15 | mark.setId(5);
16 |
17 | alice.setId(1);
18 | alice.setFirstname("Alice");
19 | alice.setLastname("W.");
20 |
21 | System.out.println("ID: " + alice.getId());
22 | System.out.println("FIRSTNAME: " + alice.getFirstname());
23 | System.out.println("LASTNAME: " + alice.getLastname());
24 |
25 | System.out.println("Instances Count: " + Student.getInstancesCount());
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/maps/MapDemo.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.maps;
2 |
3 | import java.util.HashMap;
4 | import java.util.Iterator;
5 | import java.util.Map;
6 |
7 | public class MapDemo {
8 |
9 | public static void main(String[] args) {
10 | Map map = new HashMap<>();
11 |
12 | map.put(1, "Alice");
13 | map.put(2, "Bob");
14 | map.put(3, "Costas");
15 | map.put(4, "Demi");
16 |
17 | for (Iterator itr = map.keySet().iterator(); itr.hasNext(); ) {
18 | Integer key = itr.next();
19 | if (key != 1) {
20 | itr.remove();
21 | }
22 | }
23 |
24 | map.values().removeIf(str -> !str.startsWith("A"));
25 | map.keySet().removeIf(key -> key != 1);
26 | map.entrySet().removeIf(entry -> entry.getKey() != 1);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/accounts/model/OverdraftAccount.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.accounts.model;
2 |
3 | import gr.aueb.cf.ch15.accounts.exceptions.SsnNotValidException;
4 |
5 | public class OverdraftAccount extends Account {
6 |
7 | public OverdraftAccount() {
8 |
9 | }
10 |
11 | public OverdraftAccount(String iban, User holder, double balance) {
12 | super(iban, holder, balance);
13 | }
14 |
15 | @Override
16 | public void withdraw(double amount, String ssn) throws SsnNotValidException {
17 | try {
18 | if (!isSsnValid(ssn)) {
19 | throw new SsnNotValidException(ssn);
20 | }
21 | setBalance(getBalance() - amount);
22 | } catch (SsnNotValidException e) {
23 | System.err.println("Error " + e.getMessage());
24 | throw e;
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/MinValueApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Ο χρήστης δίνει δύο ακεραίους.
7 | * Θέλουμε να υπολογίζει τον
8 | * μικρότερο των δύο ακεραίων.
9 | */
10 | public class MinValueApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int num1 = 0;
15 | int num2 = 0;
16 | int min = 0;
17 |
18 | System.out.println("Please insert two ints");
19 | num1 = scanner.nextInt();
20 | num2 = scanner.nextInt();
21 |
22 | /* if (num1 < num2) {
23 | min = num1;
24 | } else {
25 | min = num2;
26 | }*/
27 |
28 | min = (num1 < num2) ? num1 : num2; // ? :
29 |
30 | System.out.printf("Num1: %d, Num2: %d, Min: %d", num1, num2, min);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/EqualityApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | import java.util.Locale;
4 |
5 | /**
6 | * Demonstrates equality between Strings.
7 | */
8 | public class EqualityApp {
9 |
10 | public static void main(String[] args) {
11 | String s1 = "AUEB";
12 | String s2 = "AUEB";
13 | String s3 = "aueb";
14 |
15 | if (s1.equals(s3)) {
16 | System.out.println("Equal");
17 | } else {
18 | System.out.println("Not equal");
19 | }
20 | if (s1.equalsIgnoreCase(s3)) {
21 | System.out.println("EQUAL");
22 | }
23 |
24 | if (s1.compareToIgnoreCase(s3) > 0 ) {
25 |
26 | }
27 |
28 | s3 = s3.toUpperCase();
29 | s2 = s2.toUpperCase();
30 | String s4 = s3.toLowerCase();
31 | String s5 = String.valueOf('a');
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch10/SmallestPositive2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch10;
2 |
3 | import java.util.Arrays;
4 | import java.util.HashSet;
5 | import java.util.stream.Collectors;
6 | import java.util.stream.IntStream;
7 |
8 | /**
9 | * Finds the smallest missing positive.
10 | */
11 | public class SmallestPositive2 {
12 |
13 | public static void main(String[] args) {
14 | int[] arr = {1, 2, 3, 4, 6, 7};
15 |
16 | // Should return 5
17 | System.out.println(getSmallestPositive(arr));
18 | }
19 |
20 | public static int getSmallestPositive(int[] arr) {
21 | HashSet set = Arrays.stream(arr).boxed().collect(Collectors.toCollection(HashSet::new));
22 | return IntStream.rangeClosed(1, arr.length + 1)
23 | .filter(i -> !set.contains(i))
24 | .findFirst()
25 | .orElse(-1);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/PointApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15;
2 |
3 | public class PointApp {
4 |
5 | public static void main(String[] args) {
6 | Point p = new Point(12);
7 | Point p2 = new Point2D(4, 6);
8 | Point p3 = new Point3D(1, 2, 3);
9 |
10 | // p2.setY(5);
11 |
12 | // p2.movePlusOne();
13 | //
14 | // p3.movePlus10();
15 | //
16 | // ((Point2D) p2).setY(5);
17 | //
18 | // System.out.println(p);
19 |
20 |
21 | // doMovePlus10(p);
22 | // doMovePlus10(p2);
23 | // doMovePlus10(p3);
24 |
25 | doPrint(p);
26 | doPrint(p2);
27 | doPrint(p3);
28 | }
29 |
30 | public static void doMovePlus10(Point point) {
31 | point.movePlus10();
32 | }
33 |
34 | public static void doPrint(Point point) {
35 | System.out.println(point);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ReplaceArrayElements.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Demonstrates 'update' of array
5 | * elements.
6 | */
7 | public class ReplaceArrayElements {
8 |
9 | public static void main(String[] args) {
10 | int[] grades = {7, 8, 6, 9, 4, 5};
11 | increaseByOne(grades);
12 |
13 | for (int grade : grades) {
14 | System.out.print(grade + " ");
15 | }
16 | }
17 |
18 | /**
19 | * Traverses and updates each array
20 | * element increasing by one.
21 | *
22 | * @param arr the source array.
23 | */
24 | public static void increaseByOne(int[] arr) {
25 | if (arr == null) return;
26 |
27 | for (int i = 0; i < arr.length; i++) {
28 | arr[i]++;
29 | // arr[i] = arr[i] + 1;
30 | // arr[i] += 1;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/BingoIfApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Ο χρήστης δίνει τιμές μέχρι να βρει
7 | * το secret.
8 | */
9 | public class BingoIfApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int inputNum = 0;
14 | final int SECRET = 12;
15 | boolean isBingo = false;
16 |
17 | do {
18 | System.out.println("Please insert a num");
19 | inputNum = scanner.nextInt();
20 |
21 | if (inputNum == SECRET) {
22 | System.out.println("Bingo!");
23 | isBingo = true;
24 | } else {
25 | System.out.println("Not found");
26 | }
27 | } while (!isBingo);
28 | System.out.println("Thanks for using Bingo Game");
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/SymmetricArray.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class SymmetricArray {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {1, 2, 2, 1, 1};
7 | System.out.println("Array is symmetric: " + isArraySymmetric(arr));
8 | }
9 |
10 | public static boolean isArraySymmetric(int[] arr) {
11 | boolean isSymmetric = true;
12 |
13 | /*for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
14 | if (arr[i] != arr[j]) {
15 | isSymmetric = false;
16 | break;
17 | }
18 | }*/
19 | int n = arr.length - 1;
20 | for (int i = 0; i < n / 2 ; i++) {
21 | if (arr[i] != arr[n - i]) {
22 | isSymmetric = false;
23 | break;
24 | }
25 | }
26 |
27 | return isSymmetric;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch3/LeapYearApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /** Ελέγχει αν ένα έτος είναι δίσεκτο.
6 | * Δίσεκτο είναι ένα έτος αν διαιρείται με το 4
7 | * και είτε δε διαιρείται με το 100 ή διαιρείται με το 100 και το 400.
8 | */
9 | public class LeapYearApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner in = new Scanner(System.in);
13 | int year = 0;
14 | boolean isLeap = false;
15 |
16 | System.out.println("Please insert the year");
17 | year = in.nextInt();
18 |
19 | /*if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
20 | isLeap = true;
21 | }*/
22 |
23 | isLeap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
24 |
25 | System.out.println("Year: " + year + " is leap: " + isLeap);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch2/DateApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Διαβάζει τρεις ακέραιους αριθμούς που αναπαριστούν
7 | * ημέρα, μήνα, έτος και εμφανίζει σε μορφή ΗΗ/ΜΜ/ΕΕ.
8 | * Π.χ., αν δώσουμε 5 12 2022 θα εμφανίσει 05/12/22.
9 | */
10 | public class DateApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int day = 0;
15 | int month = 0;
16 | int year = 0;
17 | int formattedYear = 0;
18 |
19 | System.out.print("Please insert three integers for day, month, year: ");
20 | day = scanner.nextInt();
21 | month = scanner.nextInt();
22 | year = scanner.nextInt();
23 | formattedYear = year % 100;
24 |
25 | System.out.printf("%02d/%02d/%2d\n", day, month, formattedYear);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/marker/CompactDisk.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.marker;
2 |
3 | public class CompactDisk implements Item {
4 | private int id;
5 | private String title;
6 |
7 | public CompactDisk() {
8 |
9 | }
10 |
11 | public CompactDisk(int id, String title) {
12 | this.id = id;
13 | this.title = title;
14 | }
15 |
16 | public int getId() {
17 | return id;
18 | }
19 |
20 | public void setId(int id) {
21 | this.id = id;
22 | }
23 |
24 | public String getTitle() {
25 | return title;
26 | }
27 |
28 | public void setTitle(String title) {
29 | this.title = title;
30 | }
31 |
32 | @Override
33 | public String toString() {
34 | return "CompactDisk{" +
35 | "id=" + id +
36 | ", title='" + title + '\'' +
37 | '}';
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch7/StrBuilderApp_13.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch7;
2 |
3 | import java.time.LocalDateTime;
4 |
5 | public class StrBuilderApp_13 {
6 |
7 | public static void main(String[] args) {
8 | String s = "";
9 | StringBuilder sb = new StringBuilder();
10 | long timeElapsed;
11 | long start, end;
12 |
13 | start = System.currentTimeMillis();
14 | for (int i = 1; i < 100_000; i++) {
15 | s += i;
16 | }
17 | end = System.currentTimeMillis();
18 | System.out.printf("String Elapsed: %.3f sec.\n", (end - start) / 1000.0);
19 |
20 | start = System.currentTimeMillis();
21 | for (int i = 1; i < 100_000; i++) {
22 | sb = sb.append(i);
23 | }
24 | end = System.currentTimeMillis();
25 | System.out.printf("SB Elapsed: %.3f sec.", (end - start) / 1000.0);
26 |
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/StringsWithoutTryCatchApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | public class StringsWithoutTryCatchApp {
4 |
5 | public static void main(String[] args) {
6 | String[] tokens = {"LightBlue", "Yellow", "Red"};
7 | String s;
8 | String str = "Blue";
9 |
10 | s = getStringOrNull(tokens, str);
11 | System.out.printf("%s", (s == null) ? "Substring not found" : s);
12 | }
13 |
14 | /**
15 | *
16 | * @param strArray
17 | * @param str
18 | * @return
19 | */
20 | public static String getStringOrNull(String[] strArray, String str) {
21 | String strToReturn = null;
22 |
23 | if (strArray == null || str == null) return null;
24 |
25 | for (String s : strArray) {
26 | if (s.contains(str)) {
27 | strToReturn = s;
28 | }
29 | }
30 | return strToReturn;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/accounts/model/OverdraftJointAccount.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.accounts.model;
2 |
3 | import gr.aueb.cf.ch15.accounts.exceptions.SsnNotValidException;
4 |
5 | public class OverdraftJointAccount extends OverdraftAccount {
6 | private User secondHolder;
7 |
8 | public OverdraftJointAccount() {
9 |
10 | }
11 |
12 | public OverdraftJointAccount(User secondHolder) {
13 | this.secondHolder = secondHolder;
14 | }
15 |
16 | public OverdraftJointAccount(String iban, User holder, double balance, User secondHolder) {
17 | super(iban, holder, balance);
18 | this.secondHolder = secondHolder;
19 | }
20 |
21 | @Override
22 | public void withdraw(double amount, String ssn) throws SsnNotValidException {
23 | if (secondHolder.getSsn().equals(ssn)) {
24 | setHolder(secondHolder);
25 | }
26 | super.withdraw(amount,ssn);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch20/regex/DateApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch20.regex;
2 |
3 | import java.util.regex.Matcher;
4 | import java.util.regex.Pattern;
5 |
6 | public class DateApp {
7 |
8 | public static void main(String[] args) {
9 | String date = "12/09/2016";
10 |
11 | Pattern pattern = Pattern.compile("(\\d{2})/(\\d{2})/(\\d{4})");
12 | Matcher matcher = pattern.matcher(date);
13 |
14 | if (matcher.matches()) {
15 | String wholeDate = matcher.group(0);
16 | String day = matcher.group(1);
17 | String month = matcher.group(2);
18 | String year = matcher.group(3);
19 |
20 | String enDate = month + "/" + day + "/" + year;
21 | System.out.println("Whole Date: " + wholeDate);
22 | System.out.println("English Date Format: " + enDate);
23 | } else {
24 | System.out.println("No matches found");
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/readme.md:
--------------------------------------------------------------------------------
1 | **1η Άσκηση**
2 |
3 | Γράψτε ένα πρόγραμμα, νέα κλάση με όνομα MenuApp μέσα στο package gr.aueb.gr.ch1, που να εκτυπώνει το παρακάτω Μενού (χωρίς το κόκκινο πλαίσιο):
4 |
5 | Επιλέξτε μία από τις παρακάτω επιλογές:
6 | 1. Εισαγωγή
7 | 2. Διαγραφή
8 | 3. Αναζήτηση
9 | 4. Ενημέρωση
10 | 5. Έξοδος
11 | Δώστε αριθμό επιλογής:
12 |
13 | **2η Άσκηση**
14 | Γράψτε μία κλάση με όνομα InitialsApp μέσα στο package gr.aueb.cf.ch1, που να εκτυπώνει τα αρχικά του ονόματός σας με κεφαλαία γράμματα, με αστεράκια, π.χ.
15 |
16 | 
17 |
18 | **3η Άσκηση**
19 | Γράψτε ένα πρόγραμμα που δηλώνει δύο μεταβλητές ακεραίων num1 και num2 και αποθηκεύει το αποτέλεσμα της πρόσθεσης num1 + num2 στην μεταβλητή sum. Στη συνέχεια εκτυπώνει με println το εξής κείμενο: Το αποτέλεσμα της πρόσθεσης είναι ίσο με [το αποτέλεσμα της πρόσθεσης]
20 |
21 | Δοκιμαστικά Δεδομένα:
22 | num1 = 19 και num2 = 30
23 | Αναμενόμενα Αποτέλεσμα: 49
24 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/SelectionSort.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class SelectionSort {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {9, 4, 8, 12, 3, 23, 2};
7 | int min;
8 | int minPosition;
9 | int tmp;
10 |
11 | for (int i = 0; i < arr.length - 1; i++) {
12 |
13 | min = arr[i];
14 | minPosition = i;
15 |
16 | for (int j = i + 1; j < arr.length; j++) {
17 | if (arr[j] < min) {
18 | min = arr[j];
19 | minPosition = j;
20 | }
21 | }
22 |
23 | swap(arr, i, minPosition);
24 |
25 | }
26 |
27 | for (int num : arr) {
28 | System.out.print(num + " ");
29 | }
30 | }
31 |
32 | public static void swap(int[] arr, int i, int j) {
33 | int tmp = arr[i];
34 | arr[i] = arr[j];
35 | arr[j] = tmp;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/dlist/Node.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.dlist;
2 |
3 | public class Node {
4 | private T value;
5 | private Node next;
6 | private Node prev;
7 |
8 | public Node() {
9 |
10 | }
11 |
12 | public Node(T value) {
13 | this.value = value;
14 | }
15 |
16 | public T getValue() {
17 | return value;
18 | }
19 |
20 | public void setValue(T value) {
21 | this.value = value;
22 | }
23 |
24 | public Node getNext() {
25 | return next;
26 | }
27 |
28 | public void setNext(Node next) {
29 | this.next = next;
30 | }
31 |
32 | public Node getPrev() {
33 | return prev;
34 | }
35 |
36 | public void setPrev(Node prev) {
37 | this.prev = prev;
38 | }
39 |
40 | @Override
41 | public String toString() {
42 | return "Node{" +
43 | "value=" + value +
44 | '}';
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/CircularArrayRotation.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class CircularArrayRotation {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 |
10 | public static int[] shiftRightBy(int[] arr, int offset) {
11 | if (arr == null) return null;
12 | if (offset < 0) return null;
13 | int[] rotated = new int[arr.length];
14 |
15 | for (int i = 0; i < arr.length; i++) {
16 | rotated[(i + offset) % arr.length] = arr[i];
17 | }
18 | return rotated;
19 | }
20 |
21 | public static int[] shiftLeftBy(int[] arr, int offset) {
22 | if (arr == null) return null;
23 | if (offset < 0) return null;
24 | int[] rotated = new int[arr.length];
25 |
26 | for (int i = 0; i < arr.length; i++) {
27 | rotated[i] = arr[(i + offset) % arr.length];
28 | }
29 | return rotated;
30 | }
31 |
32 |
33 |
34 |
35 |
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/InputMismatchException2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.InputMismatchException;
4 | import java.util.Scanner;
5 |
6 | public class InputMismatchException2App {
7 |
8 | public static void main(String[] args) {
9 | Scanner in = new Scanner(System.in);
10 | int inputNum = 0;
11 | final int MAGIC_SECRET = 12;
12 |
13 | while (true) {
14 | System.out.println("Please insert the magic num");
15 |
16 | // State-testing
17 | if (!in.hasNextInt()) {
18 | System.out.println("Invalid token");
19 | in.nextLine();
20 | continue;
21 | }
22 |
23 | inputNum = in.nextInt();
24 | System.out.println("You inserted: " + inputNum);
25 | if (inputNum == MAGIC_SECRET) {
26 | System.out.println("Success!!!");
27 | break;
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/Point2D.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15;
2 |
3 | public class Point2D extends Point {
4 | private double y;
5 |
6 | public Point2D() {
7 | // super();
8 | // y = 0.0;
9 | }
10 |
11 | public Point2D(double x, double y) {
12 | super(x);
13 | this.y = y;
14 | }
15 |
16 | public double getY() {
17 | return y;
18 | }
19 | public void setY(double y) {
20 | this.y = y;
21 | }
22 |
23 | // @Override
24 | // public String convertToString() {
25 | // return super.convertToString() + "(" + y + ")";
26 | // }
27 |
28 | @Override
29 | public String toString() {
30 | return super.toString() + "(" + y + ")";
31 | }
32 |
33 | @Override
34 | public void movePlus10() {
35 | super.movePlus10();
36 | y += 10;
37 | }
38 |
39 | @Override
40 | protected void movePlusOne() {
41 | super.movePlusOne();
42 | y += 1;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch5/PowerApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch5;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Power a^n Demo.
7 | */
8 | public class PowerApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner in = new Scanner(System.in);
12 | int a = 0;
13 | int n = 0;
14 | int result = 0;
15 |
16 | System.out.println("Please insert base and power");
17 | a = in.nextInt();
18 | n = in.nextInt();
19 |
20 | result = pow(a, n);
21 |
22 | System.out.printf("%d^%d = %d", a, n, result);
23 | }
24 |
25 |
26 | /**
27 | * Returns the result of a^n.
28 | *
29 | * @param a the base.
30 | * @param n the power.
31 | * @return the result.
32 | */
33 | public static int pow(int a, int n) {
34 | int result = 1;
35 |
36 | for (int i = 1; i <= n; i++) {
37 | result *= a;
38 | }
39 |
40 | return result;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/InputMismatchExceptionApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.InputMismatchException;
4 | import java.util.Scanner;
5 |
6 | public class InputMismatchExceptionApp {
7 |
8 | public static void main(String[] args) {
9 | Scanner in = new Scanner(System.in);
10 | int inputNum = 0;
11 | final int MAGIC_SECRET = 12;
12 |
13 | while (true) {
14 | try {
15 | System.out.println("Please insert the magic num");
16 | inputNum = in.nextInt();
17 | System.out.println("You inserted: " + inputNum);
18 | if (inputNum == MAGIC_SECRET) {
19 | System.out.println("Success!!!");
20 | break;
21 | }
22 | } catch (InputMismatchException e) {
23 | System.out.println("Error: Invalid input token");
24 | in.nextLine(); // recover
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/ArrayUtil_13.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | import java.util.Arrays;
4 |
5 | public class ArrayUtil_13 {
6 |
7 | public static void main(String[] args) {
8 | int[] arr = {1, 2, 3, 6, 4, 5};
9 |
10 | // Sort
11 | Arrays.sort(arr);
12 | traverse(arr);
13 |
14 | System.out.println();
15 | // binary search to sorted array
16 | int positionOf1 = Arrays.binarySearch(arr, 1);
17 | System.out.println("POSITION OF 1: " + positionOf1);
18 |
19 | int[] arrayCopy = Arrays.copyOf(arr, arr.length); // 0 to length exclusive
20 | traverse(arrayCopy);
21 |
22 | System.out.println();
23 | int[] arrayRangeCopy = Arrays.copyOfRange(arr, 1, arr.length); // 1 to length exclusive
24 | traverse(arrayRangeCopy);
25 | }
26 |
27 | public static void traverse(int[] arr) {
28 | for (int item : arr) {
29 | System.out.print(item + " ");
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/abstractclasses/Animal.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.abstractclasses;
2 |
3 | public abstract class Animal {
4 | private long id;
5 | private String name;
6 |
7 | public Animal() {
8 |
9 | }
10 |
11 | public Animal(long id, String name) {
12 | this.id = id;
13 | this.name = name;
14 | }
15 |
16 | public long getId() {
17 | return id;
18 | }
19 | public void setId(long id) {
20 | this.id = id;
21 | }
22 | public String getName() {
23 | return name;
24 | }
25 | public void setName(String name) {
26 | this.name = name;
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "Animal{" +
32 | "id=" + id +
33 | ", name='" + name + '\'' +
34 | '}';
35 | }
36 |
37 | public abstract void speak();
38 |
39 | public void eat() {
40 | System.out.println(this.getClass().getSimpleName() + " eating");
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dto/MobileContactInsertDTO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dto;
2 |
3 | public class MobileContactInsertDTO extends BaseDTO {
4 | private UserDetailsInsertDTO userDetailsInsertDTO;
5 | private String phoneNumber;
6 |
7 | public MobileContactInsertDTO() {
8 |
9 | }
10 |
11 | public MobileContactInsertDTO(UserDetailsInsertDTO userDetailsInsertDTO, String phoneNumber) {
12 | this.userDetailsInsertDTO = userDetailsInsertDTO;
13 | this.phoneNumber = phoneNumber;
14 | }
15 |
16 | public UserDetailsInsertDTO getUserDetailsInsertDTO() {
17 | return userDetailsInsertDTO;
18 | }
19 |
20 | public void setUserDetailsInsertDTO(UserDetailsInsertDTO userDetailsInsertDTO) {
21 | this.userDetailsInsertDTO = userDetailsInsertDTO;
22 | }
23 |
24 | public String getPhoneNumber() {
25 | return phoneNumber;
26 | }
27 |
28 | public void setPhoneNumber(String phoneNumber) {
29 | this.phoneNumber = phoneNumber;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/dto/MobileContactUpdateDTO.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.dto;
2 |
3 | public class MobileContactUpdateDTO extends BaseDTO {
4 | private UserDetailsUpdateDTO userDetailsUpdateDTO;
5 | private String phoneNumber;
6 |
7 | public MobileContactUpdateDTO() {
8 |
9 | }
10 |
11 | public MobileContactUpdateDTO(UserDetailsUpdateDTO userDetailsUpdateDTO, String phoneNumber) {
12 | this.userDetailsUpdateDTO = userDetailsUpdateDTO;
13 | this.phoneNumber = phoneNumber;
14 | }
15 |
16 | public UserDetailsUpdateDTO getUserDetailsUpdateDTO() {
17 | return userDetailsUpdateDTO;
18 | }
19 |
20 | public void setUserDetailsUpdateDTO(UserDetailsUpdateDTO userDetailsUpdateDTO) {
21 | this.userDetailsUpdateDTO = userDetailsUpdateDTO;
22 | }
23 |
24 | public String getPhoneNumber() {
25 | return phoneNumber;
26 | }
27 |
28 | public void setPhoneNumber(String phoneNumber) {
29 | this.phoneNumber = phoneNumber;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/FinallyApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.InputMismatchException;
4 | import java.util.Scanner;
5 |
6 | /**
7 | * Finally is always executed!
8 | */
9 | public class FinallyApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner in = new Scanner(System.in);
13 | int num = 0;
14 |
15 | try {
16 | System.out.println("Please insert an int");
17 | num = in.nextInt();
18 |
19 | System.out.println(num);
20 | //in.close();
21 | } catch (InputMismatchException e) {
22 | e.printStackTrace();
23 | //System.out.println("Error: invalid input token");
24 | // return;
25 | } finally {
26 | try {
27 | if (in != null) in.close();
28 | } catch (Exception e) {
29 | e.printStackTrace();
30 | }
31 | }
32 |
33 | System.out.println("It's not always executed");
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/products/model/Milk.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15.products.model;
2 |
3 | public class Milk extends Product implements IMilk {
4 | private String manufacturer;
5 |
6 | public Milk() {
7 | super();
8 | manufacturer = null;
9 | }
10 |
11 | public Milk(long id, String productName, String description, double price, int quantity, String manufacturer) {
12 | super(id, productName, description, price, quantity);
13 | this.manufacturer = manufacturer;
14 | }
15 |
16 | public String getManufacturer() {
17 | return manufacturer;
18 | }
19 |
20 | public void setManufacturer(String manufacturer) {
21 | this.manufacturer = manufacturer;
22 | }
23 |
24 | @Override
25 | public String toString() {
26 | return super.toString() + " Milk{" +
27 | "manufacturer='" + manufacturer + '\'' +
28 | '}';
29 | }
30 |
31 | @Override
32 | public Milk get() {
33 | return this;
34 | }
35 | }
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/Point3D.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15;
2 |
3 | public class Point3D extends Point2D {
4 | private double z;
5 |
6 | public Point3D() {
7 |
8 | }
9 |
10 | public Point3D(double x, double y, double z) {
11 | // constructor chaining
12 | super(x, y);
13 | this.z = z;
14 | }
15 |
16 | public double getZ() {
17 | return z;
18 | }
19 |
20 | public void setZ(double z) {
21 | this.z = z;
22 | }
23 |
24 | // @Override
25 | // public String convertToString() {
26 | // return super.convertToString() + "(" + getZ() + ")";
27 | // }
28 |
29 | @Override
30 | public String toString() {
31 | return "(" + getX() + ", " + getY() + ", " + getZ() + ")";
32 | }
33 |
34 | @Override
35 | public void movePlus10() {
36 | super.movePlus10();
37 | z += 10;
38 | }
39 |
40 | @Override
41 | protected void movePlusOne() {
42 | super.movePlusOne();
43 | z += 1;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/Predicates.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Predicates = Boolean functions.
5 | */
6 | public class Predicates {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {2, 4, 6, 8, 10, 11};
10 | System.out.println("Arr all evens: " + allAreEvens(arr));
11 | }
12 |
13 |
14 | public static boolean allAreEvens(int[] arr) {
15 | if (arr == null) return false;
16 | boolean isEven = true;
17 | for (int num : arr) {
18 | if (num % 2 != 0) {
19 | isEven = false;
20 | break;
21 | }
22 | }
23 | return isEven;
24 | }
25 |
26 |
27 | public static boolean anyIsEven(int[] arr) {
28 | if (arr == null) return false;
29 | boolean isEven = false;
30 | for (int num : arr) {
31 | if (num % 2 == 0) {
32 | isEven = true;
33 | break;
34 | }
35 | }
36 | return isEven;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/IfTaxApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Υπολογίζει το φόρο ανάλογα με το
7 | * ύψος των πωλήσεων.
8 | */
9 | public class IfTaxApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | final int TAX_LIMIT = 1000;
14 | final int LOW_RATE = 10;
15 | final int HIGH_RATE = 25;
16 | int total = 0;
17 | int jan = 0;
18 | int feb = 0;
19 | int mar = 0;
20 | int apr = 0;
21 | int tax = 0;
22 |
23 | System.out.println("Please insert the amounts");
24 | jan = scanner.nextInt();
25 | feb = scanner.nextInt();
26 | mar = scanner.nextInt();
27 | apr = scanner.nextInt();
28 |
29 | total = jan + feb + mar + apr;
30 |
31 | if (total >= TAX_LIMIT) {
32 | tax = total * HIGH_RATE / 100;
33 | } else {
34 | tax = total * LOW_RATE / 100;
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/GenericApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19;
2 |
3 | import gr.aueb.cf.ch16.marker.Item;
4 |
5 | import java.util.List;
6 |
7 | public class GenericApp {
8 |
9 | public static void main(String[] args) {
10 | GenericNode genericNode = new GenericNode<>();
11 | GenericNode stringGenericNode = new GenericNode<>();
12 | Integer[] integers = {1, 2, 3, 4, 5};
13 | String[] strings = {"Hello", "World"};
14 |
15 | printArray(integers);
16 | printArray(strings);
17 |
18 | }
19 |
20 | public static void printArray(T[] arr) {
21 | for (T item : arr) {
22 | System.out.println(item);
23 | }
24 | }
25 |
26 | public static void print(GenericNode extends Number> node) {
27 | System.out.println(node.getItem());
28 | }
29 |
30 | public static void produceConsume(List super T> list, Iterable extends T> src) {
31 | for (T t : src) {
32 | list.add(t);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/WhileApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class WhileApp {
6 |
7 | public static void main(String[] args) {
8 | int i = 1;
9 | Scanner scanner = new Scanner(System.in);
10 | int choice = 0;
11 |
12 | while (i <= 10) {
13 | System.out.println("Hello");
14 | i++;
15 | }
16 |
17 | System.out.println("Please insert choice");
18 | choice = scanner.nextInt();
19 |
20 | while (choice != 5) {
21 | System.out.println("Something");
22 | System.out.println("Please insert choice");
23 | choice = scanner.nextInt();
24 | }
25 |
26 | System.out.println("Please insert choice");
27 | while ((choice = scanner.nextInt()) != 5) {
28 | System.out.println("Hello");
29 | System.out.println("Please insert choice");
30 | }
31 |
32 | while ((choice = scanner.nextInt()) == 5) {
33 |
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/Point.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15;
2 |
3 | public class Point {
4 | private double x;
5 |
6 | public Point() {
7 |
8 | }
9 |
10 | public Point(double x) {
11 | this.x = x;
12 | }
13 |
14 | public double getX() {
15 | return x;
16 | }
17 | public void setX(double x) {
18 | this.x = x;
19 | }
20 |
21 | // public String convertToString() {
22 | // return "(" + x + ")";
23 | // }
24 |
25 |
26 | @Override
27 | public String toString() {
28 | return "(" + x + ")";
29 | }
30 |
31 | public void movePlus10() {
32 | // x += 10;
33 |
34 | // self-use
35 | for (int i = 1; i <= 10; i++) {
36 | movePlusOne();
37 | }
38 | }
39 |
40 | protected void movePlusOne() {
41 | x += 1;
42 | }
43 |
44 | protected void printTypeOf() {
45 | System.out.println(this.getClass().getSimpleName());
46 | }
47 |
48 | private void reset() {
49 | x = 0;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/cfcollections/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.cfcollections;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Iterator;
5 | import java.util.List;
6 |
7 | public class Main {
8 |
9 | public static void main(String[] args) {
10 | List cities = new ArrayList<>(List.of("Athens", "Paris", "London")) ;
11 | Iterator itr = cities.iterator();
12 |
13 | for (int i = 0; i < cities.size(); i++) {
14 | System.out.println(cities.get(i));
15 | }
16 |
17 | cities.removeIf(c -> c.equals("London"));
18 |
19 | while (itr.hasNext()) {
20 | String s = itr.next();
21 | if (s.equals("London")) itr.remove(); //cities.remove(s);
22 | System.out.println(s);
23 | }
24 |
25 | // Enhanced for is based in iterator
26 | for (String s : cities) {
27 | System.out.println(s);
28 | }
29 |
30 | // forEach() is a method
31 | cities.forEach(System.out::println);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayMin2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Finds the min value of an array
5 | * by using max value;
6 | */
7 | public class ArrayMin2App {
8 |
9 | public static void main(String[] args) {
10 | int[] arr = {10, 5, 19, 3, 90};
11 | int minValue = Integer.MAX_VALUE;
12 | int minPosition = 0;
13 | int maxValue = Integer.MIN_VALUE;
14 | int maxPosition = 0;
15 |
16 | for (int i = 0; i < arr.length; i++) {
17 | if (arr[i] < minValue) {
18 | minPosition = i;
19 | minValue = arr[i];
20 | }
21 | }
22 |
23 | for (int i = 0; i < arr.length; i++) {
24 | if (arr[i] > maxValue) {
25 | maxPosition = i;
26 | maxValue = arr[i];
27 | }
28 | }
29 |
30 | System.out.printf("Min value: %d, Min position: %d", minValue, minPosition + 1);
31 | System.out.printf("Max value: %d, Max position: %d", maxValue, maxPosition + 1);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ReducingApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Reducing Demo.
5 | */
6 | public class ReducingApp {
7 |
8 | public static void main(String[] args) {
9 |
10 | }
11 |
12 | /**
13 | * Returns the sum od all the elements
14 | * of the array.
15 | *
16 | * @param arr the source array.
17 | * @return the aggregate total.
18 | */
19 | public static int getTotal(int[] arr) {
20 | if (arr == null) return 0;
21 | int total = 0;
22 | for (int item : arr) {
23 | total += item;
24 | }
25 | return total;
26 | }
27 |
28 | /**
29 | * Returns the average of the elements of
30 | * the array.
31 | * @param arr the source array.
32 | * @return the average.
33 | */
34 | public static double getAvg(int[] arr) {
35 | if (arr == null || arr.length == 0) return 0.0;
36 | // if (arr.length == 0) return 0.0;
37 |
38 | return getTotal(arr) / (double) arr.length;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/FilteringApp_6.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | /**
4 | * Filters even
5 | */
6 | public class FilteringApp_6 {
7 |
8 | public static void main(String[] args) {
9 | int[] arr = {1, 2, 3, 4};
10 | int[] returnedArr;
11 |
12 | returnedArr = filterEven(arr);
13 | printArr(returnedArr);
14 |
15 | }
16 |
17 | public static int[] filterEven(int[] arr) {
18 | int count = 0;
19 |
20 | for (int item : arr) {
21 | if (item % 2 == 0) {
22 | count++;
23 | }
24 | }
25 |
26 | int[] returnedArr = new int[count];
27 | count = 0;
28 |
29 | for (int item : arr) {
30 | if (item % 2 == 0) {
31 | returnedArr[count] = item;
32 | count++;
33 | }
34 | }
35 | return returnedArr;
36 | }
37 |
38 | public static void printArr(int[] arr) {
39 | for (int item : arr) {
40 | System.out.print(item + " ");
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Division2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Division2App {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 | int numerator = 0;
9 | int denominator = 0;
10 | int result = 0;
11 |
12 | while (true) {
13 | System.out.println("Please insert the numerator");
14 | numerator = scanner.nextInt();
15 |
16 | System.out.println("Please insert the denominator");
17 | denominator = scanner.nextInt();
18 |
19 | if (denominator == 0) {
20 | System.out.println("Error: Denominator can not be zero");
21 | break;
22 | }
23 |
24 | if (numerator == 0) {
25 | System.out.println("Error: Numerator can not be zero");
26 | break;
27 | }
28 |
29 | result = numerator / denominator;
30 | System.out.println("Result: " + result);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/MappingApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Mapping Demo.
5 | */
6 | public class MappingApp {
7 |
8 | public static void main(String[] args) {
9 | int[] salaries = {1000, 900, 1200, 1300, 2000};
10 | final double BONUS = 500.10;
11 | double[] bonusWages = getWagesWithBonus(salaries, BONUS);
12 | for (double salary : bonusWages) {
13 | System.out.print(salary + " ");
14 | }
15 | }
16 |
17 | /**
18 | * Returns the updated -with bonus, wages.
19 | *
20 | * @param wages the initial wages.
21 | * @param bonus the provided bonus.
22 | * @return a new array with the updates wages.
23 | */
24 | public static double[] getWagesWithBonus(int[] wages, double bonus) {
25 | if (wages == null) return null;
26 | double[] wagesOut = new double[wages.length];
27 |
28 | for (int i = 0; i < wages.length; i++) {
29 | wagesOut[i] = wages[i] + bonus;
30 | }
31 |
32 | return wagesOut;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/threads/Knight.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.threads;
2 |
3 | public class Knight implements IKnight, Runnable {
4 | private String name;
5 |
6 | public Knight() {
7 |
8 | }
9 |
10 | public Knight(String name) {
11 | this.name = name;
12 | }
13 |
14 | public String getName() {
15 | return name;
16 | }
17 |
18 | public void setName(String name) {
19 | this.name = name;
20 | }
21 |
22 | @Override
23 | public void embarkOnMission(IMission mission) {
24 | System.out.println(this.getName());
25 | mission.embark();
26 | }
27 |
28 | @Override
29 | public void run() {
30 | synchronized (IKnight.missions) {
31 | for (IMission mission : IKnight.missions) {
32 | if (mission.getStatus() == MissionStatus.NOT_STARTED) {
33 | this.embarkOnMission(mission);
34 | mission.setStatus(MissionStatus.STARTED);
35 | break;
36 | }
37 | }
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch1/hints.md:
--------------------------------------------------------------------------------
1 | 1. **Να προσέχετε τη στοίχιση.** Μετά από κάθε { πάμε στην επόμενη γραμμή με 1 tab δεξιά. Το IntelliJ αυτόματα τοποθετεί το σημείο εισαγωγής.
2 | 2. **Να προσέχετε τις συμβάσεις.**
3 | * **Ονόματα κλάσεων** ξεκινάνε με κεφαλαία και κάθε επόμενη λέξη με κεφαλαία (**PascalCase**), π.χ. SumApp
4 | * **Ονόματα μεταβλητών** ξεκινάνε με πεζά και κάθε επόμενη λέξη με κεφαλαία (**camelCase**), π.χ. num1
5 | 3. **Μην αφήνετε πολλές κενές γραμμές.** Αφήνετε προαιρετικά μία κενή γραμμή πριν τη main και μία γραμμή πριν από comments.
6 | 4. **Γράφετε doc comments** με τον σκοπό της κλάσης. Δε χρειάζεται να αναφέρετε στα doc comments τον τρόπο που υλοποιείται την κλάση σας.
7 | 5. **Μην αφήνετε κενή γραμμή μετά τα doc comments** και πριν την κλάση.
8 | 6. **Χρησιμοποιείται την print ή println** για να εκτυπώνεται απλό κείμενο
9 | 7. **Μπορείτε να χρησιμοποιείτε την println και για να εκτυπώσετε κείμενο με μία μεταβλητή** χρησιμοποιώντας τον **τελεστή συνένωσης +** π.χ.
10 | >System.out.println("Το αποτέλεσμα της πρόσθεσης είναι ίσο με " + sum);
11 |
12 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/SelectionSort_5.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class SelectionSort_5 {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {7, 8, 12, 21, 5, 7, 9, 10};
7 | selectionSort(arr);
8 | for (int el : arr) {
9 | System.out.print(el + " ");
10 | }
11 | }
12 |
13 | public static void selectionSort(int[] arr) {
14 | if (arr == null) return;
15 | int minPosition;
16 | int min;
17 |
18 | for (int i = 0; i < arr.length - 1; i++) {
19 | minPosition = i;
20 | min = arr[i];
21 |
22 | for (int j = i + 1; j < arr.length; j++) {
23 | if (arr[j] < min) {
24 | min = arr[j];
25 | minPosition = j;
26 | }
27 | }
28 |
29 | swap(arr, i, minPosition);
30 | }
31 | }
32 |
33 | public static void swap(int[] arr, int i, int j) {
34 | int tmp = arr[i];
35 | arr[i] = arr[j];
36 | arr[j] = tmp;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/sock/DateTimeServ.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.sock;
2 |
3 | import java.io.BufferedWriter;
4 | import java.io.IOException;
5 | import java.io.OutputStreamWriter;
6 | import java.net.InetSocketAddress;
7 | import java.net.ServerSocket;
8 | import java.net.Socket;
9 | import java.util.Date;
10 |
11 | public class DateTimeServ extends Thread {
12 | ServerSocket servFd;
13 | final int serverPort = 13;
14 |
15 | @Override
16 | public void run() {
17 | try {
18 | servFd = new ServerSocket();
19 | servFd.bind(new InetSocketAddress("127.0.0.1", serverPort), 100);
20 |
21 | for (;;) {
22 | Socket connectedFd = servFd.accept();
23 |
24 | BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(connectedFd.getOutputStream()));
25 | bw.write("Local Server says: " + new Date());
26 | bw.flush();
27 | connectedFd.close();
28 | }
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 |
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/CharReadApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.io.IOException;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * Reads two chars from standard input
8 | * and prints to console.
9 | */
10 | public class CharReadApp {
11 |
12 | public static void main(String[] args) {
13 | String s;
14 |
15 | try {
16 | System.out.println("Please insert two chars");
17 | s = getString();
18 | System.out.println(s);
19 | } catch (IOException e) {
20 | System.out.println("Error in read");
21 | }
22 | }
23 |
24 | public static String getString() throws IOException {
25 | char[] tokens = new char[2];
26 |
27 | try {
28 | for (int i = 0; i <= tokens.length; i++) {
29 | tokens[i] = (char) System.in.read();
30 | }
31 | } catch (IOException e) {
32 | // recover
33 | e.printStackTrace(); // log
34 | throw e; // rethrow
35 | }
36 | return Arrays.toString(tokens);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/GenericWhileApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Ο χρήστης δίνει αρχική τιμή, τελική τιμή,
7 | * και βήμα αύξησης (step) και υπολογίζει τον
8 | * αριθμό των επαναλήψεων (iterations).
9 | */
10 | public class GenericWhileApp {
11 |
12 | public static void main(String[] args) {
13 | Scanner scanner = new Scanner(System.in);
14 | int start = 0;
15 | int endValue = 0;
16 | int step = 0;
17 | int iterations;
18 | int j = 1;
19 |
20 | while (j <= 3) {
21 | System.out.println("Please insert beginValue, endValue and step");
22 | start = scanner.nextInt();
23 | endValue = scanner.nextInt();
24 | step = scanner.nextInt();
25 | iterations = 0;
26 |
27 | while (start <= endValue) {
28 | iterations++;
29 | start += step;
30 | }
31 | System.out.println("Iterations: " + iterations);
32 | j++;
33 | }
34 | System.out.println("Thanks for using the app");
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch15/PointsDemo.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch15;
2 |
3 | public class PointsDemo {
4 | private final static Point[] points;
5 | private static int pointsCount = 0;
6 | private static int points2DCount = 0;
7 | private static int points3DCount = 0;
8 |
9 | static {
10 | points = new Point[] {
11 | new Point(1), new Point2D(3, 4), new Point3D(1, 2, 3),
12 | new Point2D(56, 78), new Point3D(5, 6, 7)
13 | };
14 | }
15 |
16 | public static void main(String[] args) {
17 |
18 | for (Point point : points) {
19 | point.printTypeOf();
20 |
21 | if (point instanceof Point3D) {
22 | points3DCount++;
23 | } else if (point instanceof Point2D) {
24 | points2DCount++;
25 | } else {
26 | pointsCount++;
27 | }
28 | }
29 |
30 | System.out.println("Point instances: " + pointsCount);
31 | System.out.println("Point2D instances: " + points2DCount);
32 | System.out.println("Point3D instances: " + points3DCount);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch2/readme.md:
--------------------------------------------------------------------------------
1 | **Άσκηση 1**
2 |
3 | Θέλουμε να αναπτύξουμε ένα πρόγραμμα που να μετατρέπει ακέραιες θερμοκρασίες Φαρενάιτ (Fahrenheit) στην κλίμακα Κελσίου.
4 | Το πρόγραμμα θα εκτυπώνει κατάλληλο μήνυμα και θα διαβάζει στη συνέχεια την ακέραια θερμοκρασία από τον χρήστη
5 | Θα τη μετατρέπει στην κλίμακα Κελσίου σύμφωνα με τον τύπο (5/9)(fahrenheit-32).
6 |
7 | Επειδή όλα τα μέλη της παράστασης είναι int το αποτέλεσμα θα είναι int αποκόπτοντας τα δεκαδικά και δε θα είναι ακριβές, αλλά αυτό δε μας πειράζει σε αυτό το στάδιο.
8 | Προσοχή στον τρόπο που θα δώσετε την παράσταση μετατροπής. Αν δώσετε (5/9)(fahrenheit-32) τότε επειδή 5/9 είναι 0 το τελικό αποτέλεσμα θα είναι 0 (επειδή οι 5 και 9 είναι int, το αποτέλεσμα της διαίρεσης 5/9 θα είναι int δηλ. 0).
9 |
10 | Καλύτερα λοιπόν να δώσουμε την παράσταση ως: 5 * (fahrenheit-32) / 9 που είναι ακριβώς το ίδιο, αλλά αποφεύγουμε το 5/9 που δίνει 0.
11 |
12 | **Άσκηση 2**
13 |
14 | Γράψτε ένα πρόγραμμα που διαβάζει από τον χρήστη 3 ακέραιους αριθμούς που αναπαριστούν ημέρα, μήνα, έτος και την εμφανίζει σε μορφή: ΗΗ/ΜΜ/ΕΕ
15 | Π.χ. Αν δώσουμε 5 12 2022 θα πρέπει εμφανίσει 05/12/22
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/TankApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Λαμβάνει ως είσοδο true/false από το stdin, για δύο δεξαμενές
7 | * καυσίμων ενός αεροπλάνου εάν τα καύσιμα είναι κάτω από 1/4.
8 | * Επεξεργάζεται και αν η μία δεξαμενή είναι < 1/4 ανάβει η πορτοκαλί
9 | * ένδειξη, αν και οι δύο είναι < 1/4 ανάβει η κόκκινη ένδειξη.
10 | */
11 | public class TankApp {
12 |
13 | public static void main(String[] args) {
14 | Scanner scanner = new Scanner(System.in);
15 | boolean isEmptyTank1 = false;
16 | boolean isEmptyTank2 = false;
17 | boolean isOrange = false;
18 | boolean isRed = false;
19 |
20 | System.out.println("Please insert tank1 status (true / false)");
21 | isEmptyTank1 = scanner.nextBoolean();
22 |
23 | System.out.println("Please insert tank2 status (true / false)");
24 | isEmptyTank2 = scanner.nextBoolean();
25 |
26 | isOrange = isEmptyTank1 ^ isEmptyTank2;
27 | isRed = isEmptyTank1 && isEmptyTank2;
28 |
29 | System.out.println("Orange: " + isOrange + ", Red: " + isRed);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/IOCopy3.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.*;
4 |
5 | public class IOCopy3 {
6 |
7 | public static void main(String[] args) {
8 | int b;
9 | int count = 0;
10 | long start;
11 | long end;
12 | double elapsedTime;
13 | byte[] buf = new byte[8192];
14 |
15 | try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("C:/tmp/v1.mp4"));
16 | BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("C:/tmp/new-v1.mp4"))) {
17 |
18 | start = System.nanoTime();
19 | while ((b = in.read(buf)) != -1) {
20 | out.write(buf, 0, b);
21 | count += b;
22 | }
23 | end = System.nanoTime();
24 | elapsedTime = (end - start) / 1_000_000_000.0;
25 | System.out.printf("To αρχείο με μέγεθος %d KBytes (%d bytes) αντιγράφηκε\n", count / 1024, count);
26 | System.out.printf("Elapsed Time: %.2f seconds", elapsedTime);
27 | } catch (IOException e) {
28 | e.printStackTrace();
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/BankOfScotlandApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Λαμβάνει από το stdin ένα ακέραιο που
7 | * αναπαριστά λίρες Σκωτίας, τις μετατρέπει
8 | * σε δολάρια και λεπτά USD ($1 = 100cents)
9 | * και εκτυπώνει το αποτέλεσμα ως εξής, για
10 | * παράδειγμα: 10 λίρες Σκωτίας = χχ δολάρια
11 | * USD και yy cents.
12 | */
13 | public class BankOfScotlandApp {
14 |
15 | public static void main(String[] args) {
16 | int scottishPounds = 0;
17 | int usdDollars = 0;
18 | int usdCents = 0;
19 | int totalUsdCents = 0;
20 | final int PARITY = 137;
21 | Scanner scanner = new Scanner(System.in);
22 |
23 | System.out.println("Please insert the amount of Scottish pounds");
24 | scottishPounds = scanner.nextInt();
25 |
26 | totalUsdCents = scottishPounds * PARITY;
27 | usdDollars = totalUsdCents / 100;
28 | usdCents = totalUsdCents % 100;
29 |
30 | System.out.printf("\u00A3%d (Scottish Pounds) = \u0024%d (USD Dollars) and %d USD Cents",
31 | scottishPounds, usdDollars, usdCents);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch10/MissingArrayElement2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch10;
2 |
3 | /**
4 | * An array A consisting of N different integers is given.
5 | * The array contains integers in the range [1..(N + 1)], which means
6 | * that exactly one element is missing.
7 | *
8 | * Your goal is to find that missing element.
9 | *
10 | * For example, given array A such that:
11 | *
12 | * A[0] = 2
13 | * A[1] = 3
14 | * A[2] = 1
15 | * A[3] = 5
16 | *
17 | * the function should return 4, as it is the missing element.
18 | *
19 | * Write an efficient algorithm.
20 | */
21 | public class MissingArrayElement2 {
22 |
23 | public static void main(String[] args) {
24 | int[] arr = {1, 2, 4, 5, 6, 7, 8, 9, 10};
25 | System.out.println(getMissingElement(arr));
26 | }
27 |
28 | public static int getMissingElement(int[] arr) {
29 | int expectedSum = 0;
30 | int actualSum = 0;
31 | int n;
32 |
33 | n = arr.length + 1;
34 | expectedSum = n * (n + 1) / 2;
35 | for (int num : arr) {
36 | actualSum += num;
37 | }
38 | return expectedSum - actualSum;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch7/StringBuilderApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch7;
2 |
3 | /**
4 | * Compares String vs StringBuilder
5 | */
6 | public class StringBuilderApp {
7 |
8 | public static void main(String[] args) {
9 | StringBuilder sb = new StringBuilder();
10 | String s = "";
11 | long timeStart = 0L;
12 | long timeEnd = 0L;
13 | double stringElapsedTime = 0.0;
14 | double sbElapsedTime = 0.0;
15 |
16 | timeStart = System.currentTimeMillis();
17 | for (int i = 1; i <= 100_000; i++) {
18 | s = s + i;
19 | }
20 | timeEnd = System.currentTimeMillis();
21 | stringElapsedTime = (timeEnd - timeStart) / 1000.0;
22 |
23 | timeStart = System.currentTimeMillis();
24 | for (int i = 1; i <= 100_000; i++) {
25 | sb.append(i);
26 | }
27 | timeEnd = System.currentTimeMillis();
28 | sbElapsedTime = (timeEnd - timeStart) / 1000.0;
29 |
30 | System.out.println("String elapsed time: " + stringElapsedTime + " seconds");
31 | System.out.println("String Builder elapsed time: " + sbElapsedTime + " seconds");
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch16/skeletal/AbstractRectangle.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch16.skeletal;
2 |
3 | public abstract class AbstractRectangle implements IRectangle {
4 | private double width;
5 | private double height;
6 |
7 | public AbstractRectangle() {
8 |
9 | }
10 |
11 | public AbstractRectangle(double width, double height) {
12 | this.width = width;
13 | this.height = height;
14 | }
15 |
16 | public double getWidth() {
17 | return width;
18 | }
19 |
20 | public void setWidth(double width) {
21 | this.width = width;
22 | }
23 |
24 | public double getHeight() {
25 | return height;
26 | }
27 |
28 | public void setHeight(double height) {
29 | this.height = height;
30 | }
31 |
32 | @Override
33 | public double getArea() {
34 | return width * height;
35 | }
36 |
37 | @Override
38 | public double getPerimeter() {
39 | return 2 * (width + height);
40 | }
41 |
42 | @Override
43 | public boolean isSquare() {
44 | return width == height;
45 | }
46 |
47 | // @Override
48 | // public abstract String toString();
49 | }
50 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/IOCopy2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 | import java.io.IOException;
6 |
7 | public class IOCopy2 {
8 |
9 | public static void main(String[] args) {
10 | int b;
11 | int count = 0;
12 | long start;
13 | long end;
14 | double elapsedTime;
15 | byte[] buf = new byte[8192];
16 |
17 | try (FileInputStream in = new FileInputStream("C:/tmp/v1.mp4");
18 | FileOutputStream out = new FileOutputStream("C:/tmp/new-v1.mp4")) {
19 |
20 | start = System.nanoTime();
21 | while ((b = in.read(buf)) != -1) {
22 | out.write(buf, 0, b);
23 | count += b;
24 | }
25 | end = System.nanoTime();
26 | elapsedTime = (end - start) / 1_000_000_000.0;
27 | System.out.printf("To αρχείο με μέγεθος %d KBytes (%d bytes) αντιγράφηκε\n", count / 1024, count);
28 | System.out.printf("Elapsed Time: %.2f seconds", elapsedTime);
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/BinarySearch.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class BinarySearch {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {1, 2, 3, 6, 9, 12, 45, 67, 89};
7 | int low = 0;
8 | int high = arr.length - 1;
9 | int position = 0;
10 |
11 | position = binarySearch(arr, 67, low, high);
12 |
13 | if (position == -1) {
14 | System.out.println("Element not found");
15 | System.exit(1);
16 | }
17 |
18 | System.out.println("Element found in position: " + (position + 1));
19 |
20 | }
21 |
22 | public static int binarySearch(int[] arr, int value, int low, int high) {
23 | int mid = 0;
24 | if (arr == null) return -1;
25 | if (low < 0 || high > arr.length - 1) return -1;
26 | if (low > high) return -1;
27 |
28 |
29 | mid = (low + high) / 2;
30 | if (value == arr[mid]) return mid;
31 |
32 | if (value < arr[mid]) {
33 | return binarySearch(arr, value, low, mid - 1);
34 | } else {
35 | return binarySearch(arr, value, mid + 1, high);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/SwitchFallThroughApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Demo of switch/case fall through.
7 | */
8 | public class SwitchFallThroughApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner scanner = new Scanner(System.in);
12 | int grade = 0;
13 |
14 | System.out.println("Please insert grade");
15 | grade = scanner.nextInt();
16 |
17 | switch (grade) {
18 | case 1:
19 | case 2:
20 | case 3:
21 | case 4:
22 | System.out.println("Fail");
23 | break;
24 | case 5:
25 | case 6:
26 | System.out.println("Pass");
27 | break;
28 | case 7:
29 | case 8:
30 | System.out.println("Very Good");
31 | break;
32 | case 9:
33 | case 10:
34 | System.out.println("Excellent");
35 | break;
36 | default:
37 | System.out.println("Error. Grade between 1-10");
38 | break;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/BinarySearch_12.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class BinarySearch_12 {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {1, 2, 3, 6, 9, 12, 45, 67, 89};
7 | int low = 0;
8 | int high = arr.length - 1;
9 | int position = 0;
10 |
11 | position = binarySearch(arr, 67, low, high);
12 |
13 | if (position == -1) {
14 | System.out.println("Element not found");
15 | System.exit(1);
16 | }
17 |
18 | System.out.println("Element found in position: " + (position + 1));
19 |
20 | }
21 |
22 | public static int binarySearch(int[] arr, int value, int low, int high) {
23 | int mid = 0;
24 | if (arr == null) return -1;
25 | if (low < 0 || high > arr.length - 1) return -1;
26 | if (low > high) return -1;
27 |
28 |
29 | mid = (low + high) / 2;
30 | if (value == arr[mid]) return mid;
31 |
32 | if (value < arr[mid]) {
33 | return binarySearch(arr, value, low, mid - 1);
34 | } else {
35 | return binarySearch(arr, value, mid + 1, high);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch14/FlyweightApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch14;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.List;
6 |
7 | /**
8 | * Flyweight
9 | * Facade
10 | */
11 | public class FlyweightApp {
12 | private static final List points = new ArrayList<>();
13 |
14 | private FlyweightApp() {
15 |
16 | }
17 |
18 | public static ImmutablePoint getFlyweightPoint(int x, int y) {
19 | ImmutablePoint point;
20 | int position = getPointPosition(x, y);
21 |
22 | if (position == -1) {
23 | point = new ImmutablePoint(x, y);
24 | points.add(point);
25 | } else {
26 | point = points.get(position);
27 | }
28 |
29 | return point;
30 | }
31 |
32 | private static int getPointPosition(int x, int y) {
33 | int positionToReturn = -1;
34 |
35 | for (int i = 0; i < points.size(); i++) {
36 | if ((points.get(i).getX() == x) && (points.get(i).getY() == y)) {
37 | positionToReturn = i;
38 | break;
39 | }
40 | }
41 | return positionToReturn;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/IOCopy1.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.FileOutputStream;
5 | import java.io.IOException;
6 |
7 | /**
8 | * Copies a jpg/png byte-by-byte with FileInputStream read()
9 | */
10 | public class IOCopy1 {
11 |
12 | public static void main(String[] args) {
13 | int b;
14 | int count = 0;
15 | long start;
16 | long end;
17 | long elapsedTime;
18 |
19 | try (FileInputStream in = new FileInputStream("C:/tmp/aueb.jpg");
20 | FileOutputStream out = new FileOutputStream("C:/tmp/aueb-out.jpg")) {
21 |
22 | start = System.nanoTime();
23 | while ((b = in.read()) != -1) {
24 | out.write(b);
25 | count++;
26 | }
27 | end = System.nanoTime();
28 | elapsedTime = end - start;
29 | System.out.printf("To αρχείο με μέγεθος %d KBytes (%d bytes) αντιγράφηκε\n", count / 1024, count);
30 | System.out.printf("Elapsed Time: %.2f seconds", elapsedTime / 1_000_000_000.0);
31 | } catch (IOException e) {
32 | e.printStackTrace();
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch6/MaxPositionApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch6;
2 |
3 | /**
4 | * Gets the element position in an array
5 | * of the element with the max value.
6 | */
7 | public class MaxPositionApp {
8 |
9 | public static void main(String[] args) {
10 | int[] arr = {1, 2, 7, 9, 5};
11 | int maxPos;
12 |
13 | maxPos = getMaxPosition(arr, 0, arr.length - 1);
14 | if (maxPos == -1) {
15 | System.out.println("No such element");
16 | System.exit(1);
17 | }
18 | System.out.println(arr[maxPos]);
19 | }
20 |
21 | public static int getMaxPosition(int[] arr, int low, int high) {
22 | int maxPosition = 0;
23 | int maxValue;
24 |
25 | if ((arr == null) || (arr.length < 1)) return -1;
26 | if ((low < 0) || (high > arr.length - 1) || (low > high)) {
27 | return -1;
28 | }
29 |
30 | maxValue = arr[low];
31 | for (int i = low + 1; i <= high; i++) {
32 | if (arr[i] > maxValue) {
33 | maxValue = arr[i];
34 | maxPosition = i;
35 | }
36 | }
37 |
38 | return maxPosition;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/DivisionApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Λαμβάνει δύο ακεραίους και
7 | * υπολογίζει το πηλίκο.
8 | */
9 | public class DivisionApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int numerator = 0;
14 | int denominator = 0;
15 | int result = 0;
16 |
17 | while (true) {
18 | System.out.println("Please insert the numerator");
19 | numerator = scanner.nextInt();
20 |
21 | System.out.println("Please insert the denominator");
22 | denominator = scanner.nextInt();
23 |
24 | if (numerator != 0) {
25 | if (denominator != 0) {
26 | result = numerator / denominator;
27 | } else {
28 | System.out.println("Denominator can not be zero");
29 | break;
30 | }
31 | } else {
32 | System.out.println("Numerator can not be zero");
33 | break;
34 | }
35 |
36 | System.out.println("Result: " + result);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/IfAvgApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Δίνονται δύο βαθμοί και ένα σύνολο και
7 | * θα πρέπει να βρούμε το μέσο όρο.
8 | */
9 | public class IfAvgApp {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int grade1 = 0;
14 | int grade2 = 0;
15 | int total = 0;
16 | int avg = 0;
17 |
18 | System.out.println("Please insert grade1, grade2, total");
19 | grade1 = scanner.nextInt();
20 | grade2 = scanner.nextInt();
21 | total = scanner.nextInt();
22 |
23 | if (total != 0) {
24 | avg = (grade1 + grade2) / total;
25 | if (avg >= 8) { // <= 10
26 | System.out.println("Excellent");
27 | } else if (avg >= 6) {
28 | System.out.println("Very Good");
29 | } else if (avg >= 4) {
30 | System.out.println("Pass");
31 | } else { // >= 0 && <= 3
32 | System.out.println("Fail");
33 | }
34 | } else {
35 | System.out.println("Error: divide by zero!");
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/TwoDim_14.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | public class TwoDim_14 {
4 |
5 | public static void main(String[] args) {
6 | int[][] arr = new int[2][3]; // 2 rows , 4 columns
7 | arr[0][2] = 5;
8 |
9 | for (int i = 0; i < arr.length; i++) {
10 | for (int j = 0; j < arr[i].length; j++) {
11 | System.out.print(arr[i][j]+ " ");
12 | }
13 | System.out.println();
14 | }
15 |
16 | int[][] arr2 = {{1, 2}, {3, 4}, {5, 6}};
17 | for (int[] row : arr2) {
18 | for (int col : row) {
19 | System.out.print(col + " ");
20 | }
21 | System.out.println();
22 | }
23 |
24 |
25 | int[][] arr3;
26 | arr3 = new int[][] {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; // array initializer
27 |
28 |
29 | int[][] arr4 = new int[3][];
30 | arr4[0] = new int[10];
31 | arr4[1] = new int[15];
32 | arr4[2] = new int[25];
33 | for (int[] row : arr4) {
34 | for (int col : row) {
35 | System.out.print(col + " ");
36 | }
37 | System.out.println();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch17/knight/PlayTheGame.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch17.knight;
2 |
3 | public class PlayTheGame {
4 |
5 | public static void main(String[] args) {
6 | IKnight joa = new Knight();
7 | IKnight saintGeorge = new Knight();
8 |
9 | // joa.embarkOnMission(new IMission() {
10 | // @Override
11 | // public void embark() {
12 | // System.out.println("Save the princess");
13 | // }
14 | // });
15 | //
16 | // saintGeorge.embarkOnMission(new IMission() {
17 | // @Override
18 | // public void embark() {
19 | // killTheDragon();
20 | // }
21 | // });
22 |
23 | //joa.embarkOnMission(() -> System.out.println("Save the princess"));
24 | // saintGeorge.embarkOnMission(() -> killTheDragon());
25 |
26 | joa.embarkOnMission(PlayTheGame::saveThePrincess);
27 | saintGeorge.embarkOnMission(PlayTheGame::killTheDragon);
28 | }
29 |
30 | public static void killTheDragon() {
31 | System.out.println("Kill the Dragon");
32 | }
33 |
34 | public static void saveThePrincess() {
35 | System.out.println("Save the princess");
36 | }
37 |
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch2/DateToSecondsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch2;
2 |
3 | import java.util.Locale;
4 | import java.util.Scanner;
5 |
6 | /**
7 | * Λαμβάνει τρεις ακεραίους (hours, minutes, seconds
8 | * από τις 00:00) από το stdin, μετατρέπει σε seconds,
9 | * και εκτυπώνει * το σύνολο των δευτερολέπτων.
10 | */
11 | public class DateToSecondsApp {
12 |
13 | public static void main(String[] args) {
14 | Scanner scanner = new Scanner(System.in);
15 | int inputHours = 0;
16 | int inputMinutes = 0;
17 | int inputSeconds = 0;
18 | int totalSeconds = 0;
19 | final int SECS_PER_HOUR = 3600;
20 | final int SECS_PER_MIN = 60;
21 |
22 | System.out.println("Please insert hours, minutes, seconds (ints)");
23 | inputHours = scanner.nextInt();
24 | inputMinutes = scanner.nextInt();
25 | inputSeconds = scanner.nextInt();
26 |
27 | totalSeconds = inputHours * SECS_PER_HOUR + inputMinutes * SECS_PER_MIN + inputSeconds;
28 |
29 | System.out.printf(Locale.forLanguageTag("el-GR"), "%02d hours, %02d minutes, %02d seconds = %,d total seconds",
30 | inputHours, inputMinutes, inputSeconds, totalSeconds);
31 | }
32 | }
33 |
34 |
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/LightsOnApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Αποφασίζει αν θα ανάψει τα φώτα ή όχι
7 | * με βάση 1) _αν βρέχει_, 2) _αν είναι σκοτεινά_
8 | * και 3) αν η _ταχύτητα_ είναι > 100 χιλ.
9 | * Τις τιμές τις δίνει ο χρήστης από το
10 | * stdin.
11 | */
12 | public class LightsOnApp {
13 |
14 | public static void main(String[] args) {
15 | Scanner scanner = new Scanner(System.in);
16 | boolean isRaining = false;
17 | boolean isDark = false;
18 | int speed = 0;
19 | final int MAX_SPEED = 100;
20 | boolean isRunning = false;
21 | boolean lightsOn = false;
22 |
23 | System.out.println("Please insert if is raining (true/false)");
24 | isRaining = scanner.nextBoolean();
25 |
26 | System.out.println("Please insert if it is dark");
27 | isDark = scanner.nextBoolean();
28 |
29 | System.out.println("Please insert car speed (int)");
30 | speed = scanner.nextInt();
31 |
32 | isRunning = speed > MAX_SPEED;
33 | lightsOn = isRaining && (isDark || isRunning); // short-circuit
34 |
35 | System.out.println("Lights on: " + lightsOn);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/BitwiseApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class BitwiseApp {
4 |
5 | public static void main(String[] args) {
6 |
7 | }
8 |
9 | /**
10 | * Simulates the XOR operator by using
11 | * only AND, OR, NOT logical operators.
12 | *
13 | * @param b1 the first boolean value.
14 | * @param b2 the second boolean value.
15 | * @return the XOR result.
16 | */
17 | public static boolean XOR(boolean b1, boolean b2) {
18 | return (b1 && !b2) || (!b1 && b2);
19 | }
20 |
21 | /**
22 | * Returns the reverse digit (0 / 1) for
23 | * every digit of the source array. Essentially,
24 | * it returns the One's Compliment.
25 | *
26 | * @param binaryVector the source array.
27 | * @return the Compliment by one of the source array.
28 | */
29 | public static int[] complimentByOne(int[] binaryVector) {
30 | int[] binaryOut;
31 | if (binaryVector == null) return null;
32 |
33 | binaryOut = new int[binaryVector.length];
34 | for (int i = 0; i < binaryVector.length; i++) {
35 | binaryOut[i] = (binaryVector[i] == 0) ? 1 : 0;
36 | }
37 | return binaryOut;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/SearchArray_2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | /**
4 | * Searches an array with a search-value.
5 | */
6 | public class SearchArray_2 {
7 |
8 | public static void main(String[] args) {
9 | int[] grades = {8, 9, 2, 6, 10};
10 | int value = 10;
11 | int position;
12 |
13 | position = getPosition(grades, value);
14 | System.out.printf("Position: %d, Value: %d", position + 1, grades[position]);
15 | }
16 |
17 | /**
18 | * Searches the array to find a specific value. If the
19 | * value us found, the position in the array is returned.
20 | * The time complexity is linear, O(n).
21 | *
22 | * @param arr the input array.
23 | * @param value the value to search for.
24 | * @return the position if the value is found,
25 | * -1 otherwise.
26 | */
27 | public static int getPosition(int[] arr, int value) {
28 | int positionToReturn = -1;
29 |
30 | for (int i = 0; i < arr.length; i++) {
31 | if (arr[i] == value) {
32 | positionToReturn = i;
33 | break;
34 | }
35 | }
36 | return positionToReturn;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/MultipleExceptionsApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.io.File;
4 | import java.io.FileNotFoundException;
5 | import java.io.IOException;
6 | import java.util.InputMismatchException;
7 | import java.util.Scanner;
8 |
9 | public class MultipleExceptionsApp {
10 |
11 | public static void main(String[] args) {
12 | File file = new File("C:/tmp/random.txt");
13 |
14 | try (Scanner in = new Scanner(file)) {
15 | int ch = System.in.read();
16 | int num = in.nextInt();
17 | } catch (FileNotFoundException | InputMismatchException e1) {
18 | e1.printStackTrace();
19 | //System.err.println("Error. File not found");
20 | System.out.println(e1.getMessage());
21 | } catch (IOException e2) {
22 | e2.printStackTrace();
23 | System.err.println("Error. IO error");
24 | }
25 | // catch (InputMismatchException e3) {
26 | // e3.printStackTrace();
27 | // System.err.println("Error. Invalid token error");
28 | // }
29 | catch (Exception e3) {
30 | e3.printStackTrace();
31 | System.err.println("Error. Generic Exception error");
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/TwoDimArray.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Demonstrates two-dimensional arrays.
5 | */
6 | public class TwoDimArray {
7 |
8 | public static void main(String[] args) {
9 | int[][] grid = new int[3][4];
10 | int[][] arr = { {1, 2},
11 | {3, 4},
12 | {5, 6}
13 | };
14 |
15 | int[][] arr2 = new int[][] {{5, 6, 7}, {8, 9, 10}, {11, 12, 13}};
16 |
17 | // Jagged Arrays
18 | int[][] jagged = new int[3][];
19 | jagged[0] = new int[4];
20 | jagged[1] = new int[10];
21 | jagged[2] = new int[20];
22 |
23 | grid[0][0] = 5;
24 | grid[2][3] = 10;
25 |
26 | for (int i = 0; i < grid.length; i++) {
27 | for (int j = 0; j < grid[i].length; j++) {
28 | System.out.print(grid[i][j] + " ");
29 | }
30 | }
31 |
32 | for (int[] row : grid) {
33 | for (int col : row) {
34 | System.out.print(col + " ");
35 | }
36 | }
37 |
38 | for (int[] row : jagged) {
39 | for (int num : row) {
40 | System.out.print(num + " ");
41 | }
42 | }
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/NumberFormatExceptionApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Integer.parseInt(string-arithmetic value)
7 | */
8 | public class NumberFormatExceptionApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner in = new Scanner(System.in);
12 | int num = 0;
13 | String inputStr = "";
14 |
15 | System.out.println("Please insert an int");
16 | inputStr = in.nextLine();
17 |
18 | if (!isInt(inputStr)) {
19 | System.out.println("Error. Invalid token");
20 | System.exit(1);
21 | }
22 |
23 | num = Integer.parseInt(inputStr);
24 | System.out.println("Input num: " + inputStr);
25 | }
26 |
27 | /**
28 | * Returns true, if the parsing string evaluates
29 | * to integer.
30 | *
31 | * @param str the input string.
32 | * @return true, if the parsing string evaluates
33 | * to integer, false otherwise.
34 | */
35 | public static boolean isInt(String str) {
36 | try {
37 | Integer.parseInt(str);
38 | return true;
39 | } catch (NumberFormatException e) {
40 | return false;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf6/ch6/StackApp_9.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf6.ch6;
2 |
3 | /**
4 | * Stack Impl with Arrays.
5 | */
6 | public class StackApp_9 {
7 |
8 | static int[] stack = new int[100];
9 | static int pivot = -1;
10 |
11 | public static void main(String[] args) {
12 | push(1);
13 | push(2);
14 | push(3);
15 | push(4);
16 | //int num = pop();
17 | traverseStack();
18 |
19 |
20 | }
21 |
22 | public static void push(int num) {
23 | if (isFull()) {
24 | System.out.println("Stack is Full");
25 | return;
26 | }
27 |
28 | stack[++pivot] = num;
29 | }
30 |
31 | public static int pop()
32 | {
33 | if (isEmpty()) {
34 | System.out.println("Stack is empty");
35 | return Integer.MIN_VALUE;
36 | }
37 |
38 | return stack[pivot--];
39 | }
40 |
41 | public static boolean isEmpty() {
42 | return pivot == -1;
43 | }
44 |
45 | public static boolean isFull() {
46 | return pivot == stack.length - 1;
47 | }
48 |
49 | public static void traverseStack() {
50 | for (int i = pivot; i >= 0; i--) {
51 | System.out.println(stack[i] + " ");
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch9/PrintStreamMethod.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch9;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.IOException;
5 | import java.io.PrintStream;
6 | import java.io.PrintWriter;
7 | import java.nio.charset.StandardCharsets;
8 |
9 | /**
10 | * Print Stream Demo with method.
11 | */
12 | public class PrintStreamMethod {
13 |
14 | public static void main(String[] args) {
15 | try (PrintStream ps = new PrintStream("C:/tmp/f1.txt", "Windows-1252");
16 | PrintStream ps2 = new PrintStream("C:/tmp/f2.txt", StandardCharsets.UTF_8);
17 | PrintWriter pw = new PrintWriter("C:/tmp/f3.txt")) {
18 | //ps.println("Hello Coding Factory");
19 | printMsg(ps, "Γειά σου κόσμε!!!");
20 | printMsg(ps2, "Hello PrintStream 2!!!");
21 | printMsg(System.out, "Hello to Std Output!!!");
22 | pw.println("Hello from Print Writer");
23 | } catch (IOException e) {
24 | // e.printStackTrace();
25 | System.out.println("File not found");
26 | }
27 | }
28 |
29 | /**
30 | *
31 | * @param ps
32 | * @param message
33 | */
34 | public static void printMsg(PrintStream ps, String message) {
35 | ps.println(message);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch3/Menu1App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Εμφανίζεται εξακολουθητικά ένα μενού στον χρήστη
7 | * με επιλογές μέχρι να δώσει 3 (Quit).
8 | */
9 | public class Menu1App {
10 |
11 | public static void main(String[] args) {
12 | Scanner scanner = new Scanner(System.in);
13 | int choice = 0;
14 |
15 | while (true) {
16 | System.out.println("Επιλέξτε ένα από τα παρακάτω");
17 | System.out.println("1. One Player Game");
18 | System.out.println("2. Two Player Game");
19 | System.out.println("3. Έξοδος");
20 | System.out.println("Δώστε επιλογή");
21 |
22 | choice = scanner.nextInt();
23 |
24 | if (choice <= 0 || choice >= 4) {
25 | System.out.println("Λάθος επιλογή");
26 | continue;
27 | }
28 |
29 | if (choice == 3) {
30 | System.out.println("Έξοδος ...");
31 | break;
32 | }
33 |
34 | if (choice == 1) {
35 | System.out.println("One Player Game starting ...");
36 | } else { // if (choice == 2)
37 | System.out.println("Two Player Game starting ...");
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/sock/EchoCli.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.sock;
2 |
3 | import java.io.*;
4 | import java.net.InetAddress;
5 | import java.net.Socket;
6 | import java.util.Scanner;
7 |
8 | public class EchoCli {
9 |
10 | public static void main(String[] args) {
11 | Socket sockFd = null;
12 | //File file = new File("C:\\tmp\\testfd.txt");
13 |
14 | try (Scanner in = new Scanner(System.in)) {
15 | InetAddress servAddress = InetAddress.getByName("127.0.0.1");
16 | final int servPort = 7;
17 |
18 | sockFd = new Socket(servAddress, servPort);
19 | PrintWriter pw = new PrintWriter(sockFd.getOutputStream());
20 | BufferedReader br = new BufferedReader(new InputStreamReader(sockFd.getInputStream()));
21 |
22 | String line = null;
23 | do {
24 | line = in.nextLine();
25 | pw.println(line);
26 | pw.flush();
27 | System.out.println(br.readLine());
28 | } while (!line.equals("BYE"));
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | } finally {
32 | try {
33 | if (sockFd != null) sockFd.close();
34 | } catch (IOException e) {
35 | e.printStackTrace();
36 | }
37 | }
38 |
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch10/NotPaired2.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch10;
2 |
3 | /**
4 | *A non-empty array A consisting of N integers is given. The array contains an odd number
5 | * of elements, and each element of the array can be paired with another element that has
6 | * the same value, except for one element that is left unpaired.
7 | *
8 | * For example, in array A such that:
9 | *
10 | * A[0] = 9 A[1] = 3 A[2] = 9
11 | * A[3] = 3 A[4] = 9 A[5] = 7
12 | * A[6] = 9
13 | *
14 | * the elements at indexes 0 and 2 have value 9,
15 | * the elements at indexes 1 and 3 have value 3,
16 | * the elements at indexes 4 and 6 have value 9,
17 | * the element at index 5 has value 7 and is unpaired.
18 | *
19 | * For example, given array A such that:
20 | *
21 | * A[0] = 9 A[1] = 3 A[2] = 9
22 | * A[3] = 3 A[4] = 9 A[5] = 7
23 | * A[6] = 9
24 | *
25 | * the function should return 7, as explained in the example above.
26 | *
27 | * Write an efficient algorithm O(n))
28 | *
29 | */
30 | public class NotPaired2 {
31 |
32 | public static void main(String[] args) {
33 | int[] arr = new int[] {1, 1, 1};
34 | int result = 0;
35 |
36 | for (int num : arr) {
37 | // Το XOR ακυρώνει τα ζεύγη όμοιων αριθμών
38 | result ^= num;
39 | }
40 | System.out.println("Result: " + result);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch18/service/IMobileContactService.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch18.service;
2 |
3 | import gr.aueb.cf.ch18.dto.MobileContactInsertDTO;
4 | import gr.aueb.cf.ch18.dto.MobileContactUpdateDTO;
5 | import gr.aueb.cf.ch18.model.MobileContact;
6 | import gr.aueb.cf.ch18.service.exceptions.ContactNotFoundException;
7 | import gr.aueb.cf.ch18.service.exceptions.PhoneNumberAlreadyExistsException;
8 | import gr.aueb.cf.ch18.service.exceptions.UserIdAlreadyExistsException;
9 |
10 | import java.util.List;
11 |
12 | public interface IMobileContactService {
13 | MobileContact insertMobileContact(MobileContactInsertDTO mobileContactInsertDTO)
14 | throws PhoneNumberAlreadyExistsException, UserIdAlreadyExistsException;
15 |
16 | MobileContact updateMobileContact(MobileContactUpdateDTO oldDTO, MobileContactUpdateDTO newDTO)
17 | throws ContactNotFoundException, PhoneNumberAlreadyExistsException, UserIdAlreadyExistsException;
18 |
19 | void deleteMobileContactById(Long id) throws ContactNotFoundException;
20 | MobileContact getMobileContactById(Long id) throws ContactNotFoundException;
21 | List getAllMobileContacts();
22 |
23 | MobileContact getMobileContactByPhoneNumber(String phoneNumber) throws ContactNotFoundException;
24 | void deleteMobileContactByPhoneNumber(String phoneNumber) throws ContactNotFoundException;
25 | }
26 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/sock/DateTimeCli.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.sock;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStreamReader;
6 | import java.net.InetAddress;
7 | import java.net.Socket;
8 |
9 | public class DateTimeCli {
10 |
11 | public static void main(String[] args) {
12 | Socket socket = null;
13 | StringBuilder sb = new StringBuilder();
14 |
15 | try {
16 | // InetAddress servAddress = InetAddress.getByName("time.nist.gov");
17 | InetAddress servAddress = InetAddress.getByName("127.0.0.1");
18 | int servPort = 13;
19 |
20 | socket = new Socket(servAddress, servPort);
21 |
22 | BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream()));
23 | String line = "";
24 | while ((line = bf.readLine()) != null) {
25 | sb.append(line).append("\n");
26 | }
27 | //System.out.println("DateTime Server says: " + sb);
28 | System.out.println(sb);
29 | } catch (IOException e) {
30 | e.printStackTrace();
31 | } finally {
32 | try {
33 | if (socket != null) socket.close();
34 | } catch (IOException e) {
35 | e.printStackTrace();
36 | }
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/ArrayAddOneApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | public class ArrayAddOneApp {
4 |
5 | public static void main(String[] args) {
6 | int[] arr = {9, 9, 9, 9};
7 | int[] result = addOne(arr);
8 |
9 | for (int num : result) {
10 | System.out.print(num);
11 | }
12 | }
13 |
14 | public static int[] addOne(int[] arr) {
15 | if (arr == null) return null;
16 | int carry = 1;
17 | int sum;
18 | int[] arrOut = new int[arr.length + 1];
19 |
20 | for (int i = arr.length - 1; i >= 0; i--) {
21 | sum = arr[i] + carry;
22 | arrOut[i + 1] = sum % 10;
23 | carry = sum / 10;
24 | }
25 |
26 | arrOut[0] = carry;
27 | return arrOut;
28 | }
29 |
30 |
31 | public static int[] addTwoInts(int[] arr1, int[] arr2) {
32 | if (arr1 == null || arr2 == null) return null;
33 | if (arr1.length != arr2.length) return null;
34 |
35 | int carry = 0;
36 | int sum;
37 | int[] arrOut = new int[arr1.length + 1];
38 |
39 | for (int i = arr1.length - 1; i >= 0; i--) {
40 | sum = arr1[i] + arr2[i] + carry;
41 | arrOut[i + 1] = sum % 10;
42 | carry = sum / 10;
43 | }
44 |
45 | arrOut[0] = carry;
46 | return arrOut;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/Filtering.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Filtering Demo
5 | */
6 | public class Filtering {
7 |
8 | public static void main(String[] args) {
9 | int[] grades = {4, 2, 6, 9, 10, 2, 1, 1, 3, 5};
10 | final int PASS_LIMIT = 5;
11 |
12 | int[] passed = getPassGrades(grades, PASS_LIMIT);
13 | for (int grade : passed) {
14 | System.out.print(grade + " ");
15 | }
16 | }
17 |
18 | /**
19 | * Returns a new array that contains the
20 | * pass grades of the source array.
21 | *
22 | * @param grades the source array.
23 | * @param limit the threshold that
24 | * the pass grades are compared to.
25 | * @return a new array with the pass-grades.
26 | */
27 | public static int[] getPassGrades(int[] grades, int limit) {
28 | int count = 0;
29 | if (grades == null) return null;
30 | for (int grade : grades) {
31 | if (grade >= limit) {
32 | count++;
33 | }
34 | }
35 | int[] passOut = new int[count];
36 | int pivot = -1;
37 | for (int grade : grades) {
38 | if (grade >= limit) {
39 | //pivot++;
40 | passOut[++pivot] = grade;
41 | }
42 | }
43 | return passOut;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch8/ArithmeticException2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch8;
2 |
3 | import java.util.Scanner;
4 |
5 | public class ArithmeticException2App {
6 |
7 | public static void main(String[] args) {
8 | Scanner in = new Scanner(System.in);
9 | int numerator = 0;
10 | int denominator = 0;
11 | int result = 0;
12 |
13 | while (true) {
14 | System.out.println("Please insert a numerator (0 for exit) and a denominator");
15 | numerator = in.nextInt();
16 | if (numerator == 0) break;
17 | denominator = in.nextInt();
18 |
19 | try {
20 | result = div(numerator, denominator);
21 | System.out.println("Result: " + result);
22 | } catch (ArithmeticException e) {
23 | System.out.println("Error: " + e.getMessage());
24 | }
25 | }
26 | }
27 |
28 |
29 | /**
30 | *
31 | * @param numerator
32 | * @param denominator
33 | * @return
34 | */
35 | public static int div(int numerator, int denominator) {
36 | int result = 0;
37 | try {
38 | result = numerator / denominator; // Arithmetic Exception
39 | } catch (ArithmeticException e) {
40 | e.printStackTrace();
41 | throw e;
42 | }
43 | return result;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch19/maps/Main.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch19.maps;
2 |
3 | import java.util.HashMap;
4 | import java.util.Iterator;
5 | import java.util.Map;
6 |
7 | public class Main {
8 |
9 | public static void main(String[] args) {
10 | Map countries = new HashMap<>();
11 |
12 | countries.put("GR", "Greece");
13 | countries.put("FR", "France");
14 | countries.put("USA", "United States of America");
15 | countries.put("DE", "Germany");
16 |
17 | Iterator> itr = countries.entrySet().iterator();
18 | while (itr.hasNext()) {
19 | Map.Entry entry = itr.next();
20 | System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
21 | }
22 |
23 | for (Map.Entry entry : countries.entrySet()) {
24 | System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
25 | }
26 |
27 | countries.forEach((k, v) -> System.out.println(k + ", " + v));
28 |
29 | Iterator> it = countries.entrySet().iterator();
30 | while (it.hasNext()) {
31 | Map.Entry entry = it.next();
32 | if (entry.getKey().equals("FR")) {
33 | it.remove();
34 | }
35 | }
36 |
37 |
38 |
39 |
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/solutions/ch3/Menu2App.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.solutions.ch3;
2 |
3 | import java.util.Scanner;
4 |
5 | public class Menu2App {
6 | public static void main(String[] args) {
7 | Scanner in = new Scanner(System.in);
8 | int choice = 0;
9 |
10 | do {
11 | System.out.println("Επιλέξτε ένα από τα παρακάτω: ");
12 | System.out.println("1. Εισαγωγή");
13 | System.out.println("2. Διαγραφή");
14 | System.out.println("3. Ενημέρωση");
15 | System.out.println("4. Αναζήτηση");
16 | System.out.println("5. Έξοδος");
17 | System.out.print("Δώστε επιλογή: ");
18 |
19 | choice = in.nextInt();
20 |
21 | if (choice <= 0 || choice >= 6) {
22 | System.out.println("Λάθος Επιλογή");
23 | continue;
24 | }
25 |
26 | if (choice != 5) {
27 | if (choice == 1) {
28 | System.out.println("Εισαγωγή");
29 | } else if (choice == 2) {
30 | System.out.println("Διαγραφή");
31 | } else if (choice == 3) {
32 | System.out.println("Ενημέρωση");
33 | } else { // choice == 4
34 | System.out.println("Αναζήτηση");
35 | }
36 | }
37 | } while (choice != 5);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch4/SwitchApp.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch4;
2 |
3 | import java.util.Scanner;
4 |
5 | /**
6 | * Switch/case demo.
7 | */
8 | public class SwitchApp {
9 |
10 | public static void main(String[] args) {
11 | Scanner in = new Scanner(System.in);
12 | int choice = 0;
13 |
14 | do {
15 | System.out.println("Please select one of the following: ");
16 | System.out.println("1. One-player game");
17 | System.out.println("2. Two-player game");
18 | System.out.println("3. team game");
19 | System.out.println("4. Quit");
20 | System.out.println("Please insert your choice: ");
21 |
22 | choice = in.nextInt();
23 |
24 | switch (choice) {
25 | case 1:
26 | System.out.println("One-player game");
27 | break;
28 | case 2:
29 | System.out.println("Two-player game");
30 | break;
31 | case 3:
32 | System.out.println("Team game");
33 | break;
34 | case 4:
35 | System.out.println("Quit ...");
36 | break;
37 | default:
38 | System.out.println("Error in choice");
39 | break;
40 | }
41 | } while (choice != 4);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/gr/aueb/cf/ch6/MethodsWithArrays.java:
--------------------------------------------------------------------------------
1 | package gr.aueb.cf.ch6;
2 |
3 | /**
4 | * Demo with arrays as input parameters
5 | * in methods.
6 | */
7 | public class MethodsWithArrays {
8 |
9 | public static void main(String[] args) {
10 | int[] arr = {1, 2, 3, 4};
11 | printArray(arr);
12 | System.out.println();
13 | printArray(arr, 1, 3);
14 | }
15 |
16 | /**
17 | * Prints the elements of an array.
18 | *
19 | * @param arr the source array.
20 | */
21 | public static void printArray(int[] arr) {
22 | if (arr == null) return;
23 |
24 | for (int el : arr) {
25 | System.out.print(el + " ");
26 | }
27 | }
28 |
29 | /**
30 | * Overloaded version of printArray. Prints
31 | * the elements of an array in a region defined
32 | * from 'low' index to 'high' index.
33 | *
34 | * @param arr
35 | * the source array.
36 | * @param low
37 | * the 'from' index.
38 | * @param high
39 | * the 'to' index.
40 | */
41 | public static void printArray(int[] arr, int low, int high) {
42 | if (arr == null) return;
43 | if (low < 0 || high > arr.length - 1) return;
44 | if (low > high) return;
45 |
46 | for (int i = low; i <= high; i++) {
47 | System.out.print(arr[i] + " ");
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------