Question

In: Computer Science

Objects, classes, relationships, attributes, and methods Explain these five terms and how they are related. Then...

Objects, classes, relationships, attributes, and methods

  • Explain these five terms and how they are related.
  • Then provide an example to illustrate the main ideas.

Solutions

Expert Solution

  1. . Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
  2. Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
  3. Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members"
  4. Method in object-oriented programming is a procedure associated with a class. A method defines the behavior of the objects that are created from the class. Another way to say this is that a method is an action that an object is able to perform.
  5. One of the advantages of Object-Oriented programming language is code reuse. This reusability is possible due to the relationship b/w the classes. Object oriented programming generally support 4 types of relationships that are: inheritance , association, composition and aggregation.
  6. for example inhertiance, here we create a child class that can acces the functions and members of its parent class, thus these two classes have relationship between them

public class Car {

  // Create a fullThrottle() method

  public void fullThrottle() {

    System.out.println("The car is going as fast as it can!");

  }

  // Create a speed() method and add a parameter

  public void speed(int maxSpeed) {

    System.out.println("Max speed is: " + maxSpeed);

  }

  // Inside main, call the methods on the myCar object

  public static void main(String[] args) {

    Car myCar = new Car();     // Create a myCar object

    myCar.fullThrottle();      // Call the fullThrottle() method

myCar.speed(200);          // Call the speed() method

// The car is going as fast as it can!// Max speed is: 200

We created a custom Car class with the class keyword.

2) We created the fullThrottle() and speed() methods in the Car class.

3) The fullThrottle() method and the speed() method will print out some text, when they are called.

4) The speed() method accepts an int parameter called maxSpeed - we will use this in 8).

5) In order to use the Car class and its methods, we need to create an object of the Car Class.

6) Then, go to the main() method, which you know by now is a built-in Java method that runs your program (any code inside main is executed).

7) By using the new keyword we created a Car object with the name myCar.

8) Then, we call the fullThrottle() and speed() methods on the myCar object, and run the program using the name of the object (myCar), followed by a dot (.), followed by the name of the method (fullThrottle(); and speed(200);). Notice that we add an int parameter of 200 inside the speed() method.


Related Solutions

How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
Given the following list of classes, attributes and methods, - identify which items are classes, which...
Given the following list of classes, attributes and methods, - identify which items are classes, which items are attributes and which items are methods; - identify which class each attribute and method belongs to; and - suggest a class hierarchy given your list of classes. *Note - no particular capitalization scheme is used in the list below to differentiate between classes, methods, and attributes. LandOnStatue, NumberOfLegs, Height, ShoeSize, Eat, Animal, Speak, WingSpan, Age, Peck, Sleep, Horse, LengthOfMane, Move, BeakLength, LengthOfTail,...
describe the saudi aramco company in terms of its input and output components, attributes, and relationships
describe the saudi aramco company in terms of its input and output components, attributes, and relationships
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
How algorithms address object-oriented classes and objects. What is the File object? How are File objects...
How algorithms address object-oriented classes and objects. What is the File object? How are File objects used in algorithms? 175 words minumum please :)
Identify five main attributes for each of the following entity classes: a. automobile parts: b. airline...
Identify five main attributes for each of the following entity classes: a. automobile parts: b. airline flights: c. houses: d. insurance policies: e. business expense (such as what an employee may incur on a business trip):
Explain what classes and objects are in object - oriented programming. Give an example of each...
Explain what classes and objects are in object - oriented programming. Give an example of each and explain how they work together in a computer program.
Briefly explain the terms authority and legitimacy. How are the two concepts related?
Briefly explain the terms authority and legitimacy. How are the two concepts related?
discuss any five types of supplier relationship using examples to explain how the relationships can be...
discuss any five types of supplier relationship using examples to explain how the relationships can be applied in business transactions
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt user for input. When done it should give an option to compute another GPA or exit.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT