public static void main(string[] args) throws FileNotFoundException { ArrayList<Vehicle> arrayofvehicles = new ArrayList<Vehicle>();

Similar documents
DM503 Programming B. Peter Schneider-Kamp.

Lecture 2: Java & Javadoc

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Distributed Systems Recitation 1. Tamim Jabban

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

DM550 Introduction to Programming part 2. Jan Baumbach.

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Distributed Systems Recitation 1. Tamim Jabban

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

CS1150 Principles of Computer Science Objects and Classes

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Informatik II (D-ITET) Tutorial 6

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Programming in the Large II: Objects and Classes (Part 2)

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Inheritance (Part 5) Odds and ends

IT 313 Advanced Application Development Midterm Exam

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

JAVA MOCK TEST JAVA MOCK TEST II

Classes and Inheritance Extending Classes, Chapter 5.2

Java Fundamentals (II)

if (i % 2 == 0) { return 4 + method(i + 5) } if (i % 2 == 1) { return 2 + method(i + 3) } public static int method( int i )

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS-202 Introduction to Object Oriented Programming

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

C12a: The Object Superclass and Selected Methods

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Object-Oriented Programming (Java)

CH. 2 OBJECT-ORIENTED PROGRAMMING

Java Object Oriented Design. CSC207 Fall 2014

Inheritance (Part 2) Notes Chapter 6

Java Classes, Inheritance, and Interfaces

More about inheritance

F I N A L E X A M I N A T I O N

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

Polymorphism. return a.doublevalue() + b.doublevalue();

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Inheritance and Polymorphism

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

More on Objects in JAVA TM

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Inheritance. Transitivity

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS260 Intro to Java & Android 03.Java Language Basics

Making New instances of Classes

Index COPYRIGHTED MATERIAL

QUESTIONS FOR AVERAGE BLOOMERS

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Informatik II Tutorial 6. Subho Shankar Basu

Introduction to Inheritance

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

javagently jg3e H:\ ProgTwo Programming II Lecture 7 A case study (class containing an array) and model diagrams. 02/02/2003 Dr Andy Brooks 1

Programming overview

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Inheritance and Polymorphism. CSE 114, Computer Science 1 Stony Brook University

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Abstract Classes and Interfaces

Inheritance (IS A Relationship)

Software and Programming 1

Chapter 14 Abstract Classes and Interfaces

Rules and syntax for inheritance. The boring stuff

Overriding Variables: Shadowing

Inheritance. The Java Platform Class Hierarchy

Accelerating Information Technology Innovation

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

COMP 250: Java Object Oriented Programming

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Class, Variable, Constructor, Object, Method Questions

Introduction to Computer Science II (CSI 1101) Midterm Examination

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Arrays Classes & Methods, Inheritance

CMSC131 Final Exam Practice Questions

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Full file at Chapter 2 - Inheritance and Exception Handling

Create a Java project named week9

CIS 110: Introduction to computer programming

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

Java Magistère BFA

c) And last but not least, there are javadoc comments. See Weiss.

CS Programming I: Inheritance

INHERITANCE. Spring 2019

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

COP 3330 Final Exam Review

AP CS Unit 6: Inheritance Notes

Inheritance. A key OOP concept

Transcription:

import java.io.*; import java.util.*; import java.text.*; import java.lang.*; // Class n00845431 public class Project4 static class EmailComparator implements Comparator <Vehicle> public int compare(vehicle one, Vehicle two) Vehicle p1 =(Vehicle) one; Vehicle p2 =(Vehicle) two; return (p1.getemail().compareto(p2.getemail())); public static void main(string[] args) throws FileNotFoundException //creating arraylist for vechicle class File file = new File(args[0]); System.out.println(file.exists()); Scanner input = new Scanner(file); ArrayList<Vehicle> arrayofvehicles = new ArrayList<Vehicle>(); while(input.hasnextline()) String test = input.nextline(); ///if type vehicle if (test.equals("vehicle")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); // create vehicle object arrayofvehicles.add(new Vehicle(name, address, phone, email)); else if (test.equals("car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); // create car object arrayofvehicles.add(new Car(name, address, phone, email, convertible, color));

else if (test.equals("american car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); boolean detroit = input.nextboolean(); boolean union = input.nextboolean(); // create american car object arrayofvehicles.add(new AmericanCar(name, address, phone, email, convertible, color, detroit, union)); else if (test.equals("foreign car")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); boolean convertible = input.nextboolean(); input.nextline(); String color = input.nextline(); String manufacturer = input.nextline(); //boolean detroit = input.nextboolean(); //boolean union = input.nextboolean(); float importduty = input.nextfloat(); input.nextline(); input.nextline(); // create foreign car object arrayofvehicles.add(new ForeignCar(name, address, phone, email, convertible, color, manufacturer, importduty)); else if (test.equals("bicycle")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); int speed = input.nextint(); // create bicycle object arrayofvehicles.add(new Bicycle(name, address, phone, email, speed)); else if (test.equals("truck")) String name = input.nextline(); String address = input.nextline(); String phone = input.nextline(); String email = input.nextline(); float tons = input.nextfloat(); float cost = input.nextfloat(); input.nextline(); String date = input.nextline(); arrayofvehicles.add(new Truck(name, address, phone, email, tons, cost, date));

input.close(); // Calling Methods System.out.println("**********Printing Details**********"); // Calls printall System.out.println("The Array List"); printall(arrayofvehicles); // Calls Email Id System.out.println("Sorting by Email ID"); sort(arrayofvehicles); // Calls numberofentries printrecord(arrayofvehicles); // Calls justbt System.out.println("Just Bicycles and Trucks"); justbt(arrayofvehicles); // Calls areacode System.out.println("Area Code 987"); areacode(arrayofvehicles); //main // Methods //Prints the ArrayList public static void printall(arraylist<vehicle> array) for (int x = 0; x <= array.size() -1; x++) System.out.println(array.get(x).toString());

//Sorts email id in arraylist public static void sort(arraylist<vehicle> array) Collections.sort(array, new EmailComparator()); for (int i = 0; i <= array.size() -1; i++) System.out.println(array.get(i).toString()); //Prints number of entries public static void printrecord(arraylist<vehicle> array) System.out.println("The number of records: " + array.size()); //Prints number of entries public static void justbt(arraylist<vehicle> array) for (int x = 0; x <= array.size() -1; x++) if (array.get(x) instanceof Bicycle) System.out.println(array.get(x).toString()); else if (array.get(x) instanceof Truck) System.out.println(array.get(x).toString()); //Prints number of entries public static void areacode(arraylist<vehicle> array) String number; for (int x = 0; x <= array.size() -1; x++) number = array.get(x).getphone(); if (number.substring(1,4).equals("987")) System.out.println(array.get(x).toString()); //class // Vehicle as parent class class Vehicle //data members declared as private private String ownername; private String address; private String phone; private String email;

//null public Vehicle () //parameterized constructor of vechicle class public Vehicle(String ownername, String address, String phone, String email) this.ownername = ownername; this.address= address; this.phone = phone; this.email = email; //getters and setters for data members public String getownername() return ownername; public void setownername(string newownername) this.ownername = newownername; public String getaddress() return address; public void setaddress(string newaddress) this.address = newaddress; public String getphone() return phone; public void setphone(string newphone) this.phone = newphone; public String getemail() return email; public void setemail(string newemail) this.email = newemail; public boolean equals(object obj) return true; //tostring(0 returns String type contains data member values of a vehicle class

public String tostring() return "Type of Vehicle: Vehicle\n" + "Owner's name : " + ownername + "\n" + "Address : " + address + "\n" + "Phone : " + phone + "\n" + "Email : " + email; //Car is child class inherits from vechile class Car extends Vehicle //datamembers intilation as private private boolean convertible; private String color; //parameterized constructor of Car class public Car(String ownername, String address, String phone, String email, boolean convertible, String color) //calling Vechicle class constructor using super super(ownername, address, phone, email); setconvertible(convertible); setcolor(color); //getters and setters for convertible and getcolor data members of car public boolean getconvertible() return convertible; public void setconvertible(boolean newconvertible) this.convertible = newconvertible; public String getcolor() return color; public void setcolor(string newcolor) this.color = newcolor; //tostring() method returns String type And over rides parent class is Vehicle public String tostring() return super.tostring() + "Type of Vehicle: Car\n" + "Convertible : " + convertible + "\n" +

"Color : " + color; //end car class /*AmericanCar inherits Car and Car inherits vechile.i.e multilevel inhertience between vechile and AmericanCar*/ // Subclass of Car class AmericanCar extends Car //Adding two data members private boolean madedetroit; private boolean unionshop; //parameterized constructor of American car public AmericanCar(String ownername, String address, String phone, String email, boolean convertible, String color, boolean madedetroit, boolean unionshop) //calling car class parameterized constructor super(ownername, address, phone, email, convertible, color); setmadedetroit(madedetroit); setunionshop(unionshop); //getter and setters for madedetroit, unionshop data members public boolean getmadedetroit() return madedetroit; public void setmadedetroit(boolean newmadedetroit) this.madedetroit = newmadedetroit; public boolean getunionshop() return unionshop; public void setunionshop(boolean newunionshop)

this.unionshop = newunionshop; //overriding super class tostring() and returns adding with newly added variables public String tostring() return super.tostring() + "Type of Vehicle: American Car\n" + "Made in Detroit : " + madedetroit + "\n" + "Union shop : " + unionshop; //child class ForeignCar inherits from Car parent class // Subclass of Car class ForeignCar extends Car // ForeignCar data members intilization private String manufacturercountry; private float importduty; //parameterized constructor for ForeignCar public ForeignCar(String ownername, String address, String phone, String email, boolean convertible, String color, String manufacturercountry, float importduty) super(ownername, address, phone, email, convertible, color); this.manufacturercountry = manufacturercountry; this.importduty = importduty; //getters and setters for manufacturercountry, importduty public String getmanufacturercountry() return manufacturercountry;

public void setmanufacturercountry(string newmanufacturercountry) this.manufacturercountry = newmanufacturercountry; public float getimportduty() return importduty; public void setimportduty(float newimportduty) this.importduty = newimportduty; //tostring() overrides parent class car public String tostring() return super.tostring() + "Type of Vehicle: Foreign Car\n" + "Manufacturer country : " + manufacturercountry + "\n" + "Import duty : " + importduty; //child class Bicycle inherits from vehicle parent class //Bicycle is child class inherits from vechile class Bicycle extends Vehicle //initialize data member as private private int numberspeeds; //parameter constructor of Bicycle public Bicycle(String ownername, String address, String phone, String email, int numberspeeds) super(ownername, address, phone, email); this.numberspeeds = numberspeeds; //getter and setter of numberspeeds data member

public int getnumberspeeds() return numberspeeds; public void setnumberspeeds(int newnumberspeeds) this.numberspeeds = newnumberspeeds; //overriding tostring method() return String type public String tostring() return super.tostring() + "Type of Vehicle: Bicycle\n" + "Number of speeds : " + numberspeeds; //child class Truck inherits parent class Vechicle //Truck is child class inherits from vechile class Truck extends Vehicle private float numbertons; private float truckcost; private String datepurchased; //parameter constructor of Truck public Truck(String ownername, String address, String phone, String email, float numbertons, float truckcost, String datepurchased) super(ownername, address, phone, email); this.numbertons = numbertons; this.truckcost = truckcost; this.datepurchased = datepurchased; //getters and setter for Truck data members

public float getnumbertons() return numbertons; public void setnumbertons(float newnumbertons) this.numbertons = newnumbertons; public float gettruckcost() return truckcost; public void settruckcost(float newtruckcost) this.truckcost = newtruckcost; public String getdatepurchased() return datepurchased; public void setdatepurchased(string newdatepurchased) this.datepurchased = newdatepurchased; //tostring() override superclass method public String tostring() return super.tostring() + "Type of Vehicle: Truck\n" + "Number of tons : " + numbertons + "\n" + "Truck cost : " + truckcost + "\n" + "Date purchased : " + datepurchased;

/* sets, null cnstuctors, email comparator, type of vehicles, Boolean after each class... */