Question
java
Given that class Zebra has a constructor with a header as follows: public Zebra(int age, int stripes) complete the following
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Zebra {
    int age;
    int stripes;

    public Zebra(int age, int stripes) {
        this.age = age;
        this.stripes = stripes;
    }

    public static void zebraCatalog(ArrayList<Zebra> zebras, File zebraList) {
        Scanner sc;
        try {
            sc = new Scanner(zebraList);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
        while (true){
            try {
                int age = sc.nextInt();
                int stripes = sc.nextInt();
                Zebra z = new Zebra(age, stripes);
                zebras.add(z);
            }catch (Exception e){
                break;
            }
        }
    }

    public static void main(String[] args) {
        File zebraList = new File("test.txt");
        ArrayList<Zebra> ar = new ArrayList<>();
        zebraCatalog(ar, zebraList);

        for (Zebra z: ar)
            System.out.println(z.age+" "+z.stripes);
    }
}
Add a comment
Know the answer?
Add Answer to:
java Given that class Zebra has a constructor with a header as follows: public Zebra(int age,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • package simple; import java.awt.Frame; public class Scrubby extends Frame { int y; public Scrubby() { f(15);...

    package simple; import java.awt.Frame; public class Scrubby extends Frame { int y; public Scrubby() { f(15); } int f(int x) { return x+y; } } the cross-reference tool generates the following report: e:/test/ # java JavaXref simple Parsing... e:\test\simple\Scrubby.java Resolving types... resolving: simple.Scrubby Writing report... Cross Reference Report ====================== A Java Cross-Reference Tool A Java Cross-Reference Tool 3 sur 13 Package: simple ----------------------------------------- simple.Scrubby (Class) [Scrubby.java:5] Superclass: Frame [Scrubby.java:3] Imported classes/packages: Frame simple.Scrubby.~constructor~ (Method) [Scrubby.java:8] simple.Scrubby.y (Variable) [Scrubby.java:6] Type: int...

  • Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in...

    Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course {     private String cName;     private ArrayList<Subject> cores;     private ArrayList<Major> majors;     private ArrayList<Subject> electives;     private int cCredit;          public Course(String n, int cc){         cName = n;         cCredit = cc;         cores = new ArrayList<Subject>(0);         majors =...

  • use java follow my code: public class q1 {    // Instance Variables    int currentFloor;...

    use java follow my code: public class q1 {    // Instance Variables    int currentFloor;    double maximumWeight;    int topFloor;       // Constructor Declaration of Class    public q1 (int currentFloor, double maximumWeight, int topFloor)    {    this.currentFloor = currentFloor;    this.maximumWeight = maximumWeight;    this.topFloor = topFloor;    }       // Property to get value of currentFloor instance variable    public int getCurrentFloor()    {    return currentFloor;    }       // Property...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT