Pres. 2 HW
un 5, 9, 10
public class Food {
//attributes
private String cuisine;
private String culture;
private int mealsConsumed;
// Static counter variable
public static int peopleEating;
//constructor
public Food(String cuisine, String culture, int mealsConsumed)
{
this.cuisine = cuisine;
this.culture = culture;
this.mealsConsumed = mealsConsumed;
this.peopleEating = 1;
}
// static method to print out counter
public static int printPersonCounter() {
peopleEating++;
System.out.println("Person counter: " + peopleEating);
return peopleEating;
}
public String getCulture(){
return culture;
}
public void setMealCount(){
mealsConsumed++;
}
public String getCuisine(){
return cuisine;
}
public int getMeals() {
// Increment the person counter if mealsConsumed is greater than 5
if (mealsConsumed > 5) {
printPersonCounter();
}
return mealsConsumed;
}
}
public class Foodmain {
public static void main(String[] args) {
Food newFood = new Food("Dal", "Indian", 20);
System.out.println(newFood.getCulture());
System.out.println(newFood.getMeals());
}
}
Foodmain.main(null);
public class myClass{
public static ArrayList<Object> reverse (ArrayList<Object>q)
{
Object obj;
if (q.size() != 0){
obj = q.remove(0);
q = reverse(q);
q.add(obj);
}
return q;
}
public static void main(String[] args) {
ArrayList<Object> q = new ArrayList<Object>();
q.add(0, 1);
q.add(1, 2);
q.add(2, 3);
System.out.println("Original list: " + q);
myClass obj = new myClass();
ArrayList<Object> reversedList = obj.reverse(q);
System.out.println("Reversed list: " + reversedList);
}}
myClass.main(null);
public class Vehicle {
private String name;
private String sound;
// methods and attributes
public Vehicle (String name){
this.name = name;
this.sound = "Honk!";
}
public void getSound(){
System.out.println(this.sound);
}
public String getName(){
return name;
}
}
class Car extends Vehicle {
private String name;
private String sound;
public Car(String name){
super(name);
this.sound = sound;
}
public static void main(String[] args) {
Car car1 = new Car("Toyota");
car1.getSound();
System.out.println(car1.getName());
}
}
Car.main(null);
public class Entertainer{
private String name;
private String tour;
public Entertainer(){
name = "Taylor Swift";
tour = "Eras Tour!";
}
public void practice(){
System.out.println("Practicing for my show tonight on the " + this.tour);
}
public void perform(){
System.out.println("I'm excited for my performance!");
}
public void announce(){
System.out.print("and more!");
}
}
public class Singer extends Entertainer{//inheritance
private ArrayList<String> songs;
private String tour;
public Singer(){
super();
this.tour = "Eras Tour";
this.songs = new ArrayList<String>();
String[] folkloreSongs = {"the 1", "cardigan", "the last great american dynasty", "exile (feat. Bon Iver)", "my tears ricochet", "mirrorball", "seven", "august", "this is me trying", "illicit affairs", "invisible string", "mad woman", "epiphany", "betty", "peace", "hoax"};
for (int i = 0; i < folkloreSongs.length; i++){
songs.add(folkloreSongs[i]);
}
}
public void display(){
System.out.println(songs);
}
public String getSong(){
String song = "\nMy performance today is: " + songs.get((int)(Math.random()* songs.size()));
return song;
}
public void perform(){
super.perform();
System.out.print("My set list includes: ");
for(String song : songs){
System.out.print(song + ", ");
}
}
public static void main(String[] args) {
Singer taylor = new Singer();
taylor.practice();
taylor.perform();
taylor.announce();
Entertainer selena = new Singer();//polymorphism
System.out.println("\nHey, my name's Selena!" + ((Singer)selena).getSong());
taylor.display();
}}
Singer.main(null);
public class Dancer extends Entertainer{//inheritance
private int numDances;
public Dancer(int numDances){
super();
this.numDances = numDances;
}
public void perform(){
super.perform();
System.out.println("Dancing at the Eras tour!");//overriden
}
public void jete(){
System.out.println("Leaps!");
}
public void pirouette(){
System.out.println("Spins!");
}
public String toString(){
return ("I danced this many times: " + numDances);
}
public static void main(String[] args) {
Dancer bob = new Dancer(6);
System.out.println(bob.toString()); // prints "I danced this many times! 6"
bob.perform(); // prints "Dancing at the Eras tour!"
bob.jete(); // prints "Leaps!"
bob.pirouette(); // prints "Spins!"
Entertainer Randy = new Dancer(23);//polymorphism
Randy.perform();
}}
Dancer.main(null);
//Using While Loop
public class fibo {
public static void main(String[] args)
{
int tries = 10;
int num1 = 0;
int num2 = 1;
System.out.print("Fibonacci Sequence: ");
int i=1;
while(i <= tries)//base case
{
System.out.print(num1+" ");
int total = num1 + num2;
num1 = num2;
num2 = total;
i++;
}
fibo fib = new fibo();//object initilization and declaration
}
}
fibo.main(null);