Skip to content

Instantly share code, notes, and snippets.

@Malix-off
Last active November 28, 2023 13:49
Show Gist options
  • Save Malix-off/3a03ab0fc213cdc20837fa6e20690bdc to your computer and use it in GitHub Desktop.
Save Malix-off/3a03ab0fc213cdc20837fa6e20690bdc to your computer and use it in GitHub Desktop.
EFREI-2026-L3-HUNGARY-Java-1

EFREI 2026 L3 HUNGARY Java 1 (24/09/23)

  • 24/09/2023

Guidelines

    • Create a zoo (of some sorts)
    • Have a common ancestor class: animal
    • Each animal should have a name
    • Have a three other classes that extend that class (zebra, elephant, python)
    • Implement the makesound feature that we have seen on the slide.
    • The animals should have their own methods and variables (use your imagination)
    • Create an interface called hasLegs with a letsRun method and a getNumberOfLegs method and then implement that interface for the animals that have legs.
    • Create a common array for the animals (did not understood this assignement => skipped)
    • Write a program that takes a number from standard input and displays it. For this, we will use an object (an instance) of the Scanner class and particularly its nextInt() method.
    • Modify the previous program (did not understood this assignement => made a new class) so that it reads from the keyboard a series of positive or zero real numbers (corresponding to notes), ending in the value -1, and calculates the Olympic average of these values, i.e. the average of the scores regardless of the highest or lowest score.
package zoo;
public abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public abstract void makeSound();
public String getName() {
return name;
}
}
package zoo;
public class Elephant extends Animal implements HasLegs {
private int numberOfLegs;
private boolean hasTrunk;
public Elephant(String name, int numberOfLegs, boolean hasTrunk) {
super(name);
this.numberOfLegs = numberOfLegs;
this.hasTrunk = hasTrunk;
}
public void makeSound() {
System.out.println("Elephant is making a sound");
}
public void letsRun() {
System.out.println("Elephant is running.");
}
public int getNumberOfLegs() {
return numberOfLegs;
}
public boolean hasTrunk() {
return hasTrunk;
}
public void sprayWater() {
System.out.println("Elephant is spraying water.");
}
}
package zoo;
public interface HasLegs {
void letsRun();
int getNumberOfLegs();
}
package input;
import java.util.Scanner;
public class NumberDisplayProgram {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
if (scanner.hasNextInt()) {
int number = scanner.nextInt();
System.out.println("You entered: " + number);
} else {
System.out.println("Invalid input. Please enter a valid integer.");
}
scanner.close();
}
}
package input;
import java.util.InputMismatchException;
import java.util.Scanner;
public class OlympicAverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double sum = 0;
int count = 0;
System.out.println("Enter positive or zero real numbers, end with -1:");
while (true) {
try {
double input = scanner.nextDouble();
if (input == -1)
break;
if (input >= 0) {
sum += input;
count++;
} else {
System.out.println("Please enter a non-negative number.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a valid real number.");
scanner.next(); // Clear the invalid input from the scanner
}
}
double average = count > 0 ? sum / count : 0;
System.out.println("Olympic average: " + average);
scanner.close();
}
}
package zoo;
public class Python extends Animal {
private boolean isVenomous;
private int lengthInFeet;
public Python(String name, boolean isVenomous, int lengthInFeet) {
super(name);
this.isVenomous = isVenomous;
this.lengthInFeet = lengthInFeet;
}
public void makeSound() {
System.out.println("Python is making a sound");
}
public boolean isVenomous() {
return isVenomous;
}
public int getLengthInFeet() {
return lengthInFeet;
}
public void slither() {
System.out.println("Python is slithering.");
}
}
package zoo;
public class Zebra extends Animal implements HasLegs {
private int numberOfLegs;
private boolean isStriped;
public Zebra(String name, int numberOfLegs, boolean isStriped) {
super(name);
this.numberOfLegs = numberOfLegs;
this.isStriped = isStriped;
}
public void makeSound() {
System.out.println("Zebra is making a sound");
}
public void letsRun() {
System.out.println("Zebra is running.");
}
public int getNumberOfLegs() {
return numberOfLegs;
}
public boolean isStriped() {
return isStriped;
}
public void eatGrass() {
System.out.println("Zebra is eating grass.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment