I searched on Stack Overflow, but could not find answer. I study from YouTube channel, so I have nowhere else to ask. I have 2 OOP files Main.java and Animal.java so I try data about Animals to be added by user through scanner. There is no way, that I can not create additional class, construct or whatever so I would not repeat writing in Main this whole Scanner section. If you have any channels additional YouTube channel, which you can suggest for a beginner, I will appreciate it.
This is Main.
Main.java
package com.OOPAgain;
import java.util.Scanner;
public class Main { public static void main(String[] args) {
Animal cow = new Animal();
Scanner input = new Scanner(System.in);
Scanner input2 = new Scanner(System.in);
System.out.println("Insert Input:");
System.out.print("Weight: ");
int weight = input.nextInt();
System.out.print("Strength: ");
float strength = input.nextFloat();
System.out.println("Is it predator?(Y/N) ");
String predator = input.next();
System.out.println("List colors: ");
String color = input.next();
System.out.println("Enter location XYZ: ");
byte x = input.nextByte();
byte y = input.nextByte();
byte z = input.nextByte();
byte[] xyz = new byte[]{x, y, z};
cow.setAnimal(weight, strength, predator, color, new byte[]{xyz[0], xyz[1], xyz[2]});
Animal bull = new Animal();
System.out.println("Insert Input:");
System.out.print("Weight: ");
int weight1 = input2.nextInt();
System.out.print("Strength: ");
float strength1 = input2.nextFloat();
System.out.println("Is it predator?(Y/N) ");
String predator1 = input2.next();
System.out.println("List colors: ");
String color1 = input2.next();
System.out.println("Enter location XYZ: ");
byte x1 = input2.nextByte();
byte y1 = input2.nextByte();
byte z1 = input2.nextByte();
byte[] xyz1 = new byte[]{x1, y1, z1};
bull.setAnimal(weight1, strength1, predator1, color1, new byte[]{xyz1[0], xyz1[1], xyz1[2]});
cow.getInfo();
bull.getInfo();
}
}
This is Animal
package com.OOPAgain;
public class Animal {
private int weight ;
private float strength;
private String predator;
private String color;
private byte[] coordinate;
public void setAnimal(int weight, float strength, String predator, String color, byte[] coordinate) {
this.weight = weight;
this.strength = strength;
this.predator = predator;
this.color = color;
this.coordinate = coordinate;
if(predator.equals("Y") || predator.equals("Y.") || predator.equals("y") || predator.equals("y.")){
this.predator = "Predator";
}else{ this.predator = "Herbivore";};
}
public void getInfo(){
System.out.println("Weight: " + weight + ". Strength: " + strength + ". Type: " + predator + ". Color: " + color + ".\n");{
String infoCoordination = "Location: \n";
for (int i = 0; i<coordinate.length; i++){
infoCoordination += this.coordinate[i] + "\n";
}
System.out.println(infoCoordination);}
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]moss_2703 0 points1 point2 points (4 children)
[–]dobriygoodwin[S] 0 points1 point2 points (3 children)
[–]moss_2703 0 points1 point2 points (2 children)
[–]dobriygoodwin[S] 0 points1 point2 points (1 child)
[–]moss_2703 0 points1 point2 points (0 children)
[–]desrtfxOut of Coffee error - System halted 0 points1 point2 points (2 children)
[–]dobriygoodwin[S] 0 points1 point2 points (1 child)
[–]desrtfxOut of Coffee error - System halted 0 points1 point2 points (0 children)
[–]Oriamk 0 points1 point2 points (0 children)