Question

Looking for some quick and useful notes on this: • Using Classes and Objects o Creating...

Looking for some quick and useful notes on this:

• Using Classes and Objects

o Creating Objects: understand how to create objects, assign them, invoke methods on them

and re-assign references.

o The String Class: understanding and using strings and String methods

o Packages: use import statement when needed

o Wrapper Classes: use Wrapper classes to parse strings.

• Writing classes

o Anatomy of a Class: understand the concept of a class, define its instance variables,

constructor and methods.

o Anatomy of a Method: understand class methods, be able to write methods.

o Encapsulation: understand visibility modifiers, write accessors and mutators, write the

toString method.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

how to create objects, assign them, invoke methods on them and re-assign references.

We can create the objects by using “new” operator.

Classname reference ; //here we are creating the class reference

reference=new Classname(); //Creating a class object and assigning to the class reference.

Then we can call the methods of that class by using the reference.methodname();

How to re-assign the references?

Employee emp1=new Employee();

In the above step we are creating an Object to the Employee class pointing by the reference emp1.

Employee emp2;

Now we are creating another reference to the Same Employee class.

Now we can re-assign the reference of Employee object pointed by emp1 to emp2;

emp2=emp1;

Now emp2 is Pointing to the Employee object.

The String Class: understanding and using strings and String methods

String: A String represents a group of characters .

Ex: beautiful

Any string is an object of String class to java.

In java a String is String object its not character array.

There are Three ways of creating Strings.

1)We ac declare a string variable and directly assign group of characters to the string variable.

Ex:String str= ”Hello”;

Here Jvm Create object internally .Object means memory .that memoryname is str.

2)We can create String class object and store a string into that Object.

String s1=new String(“Hello”);

3) We can create a String object by converting a character array into string object.

Char arr[]={‘h’,’e’,’l’,’l’,’o’};

String s2= new String(arr);

String methods:

Some of the important String class methods are :

  1. String Concat(String str):

It concatenates the calling string with str.

Ex:

String s1=”beauiti”;

String s2=”ful”;

s1.concat(s2); //here we are joining string s2 at the end of s1.

So the if we print s1 as System.out.println(s1); it will display “beautiful”

int Length():

This method returns the length of the String.

String s1=”beautiful”;

Int n=s1.length();

Now the integer variable ‘n’ contains 9.

Packages: use import statement when needed

A package represents a subdirectory that contains a group of related classes and interfaces,enumerations and annotations.

Suppose if we want to use all the classes inside the package ,we have to use ‘*’

Import java.io.*;

Hare java.io is the package name inside that package if we want to use all the classes we have to use ‘*’

Advantages of packages:

1)A package protects classes and interfaces by hiding them.

2)Packages provide encapsulation ,that means same names can be used for the classes of two different packages.

Wrapper Classes: use Wrapper classes to parse strings

In java all wrapper classes have method to parse string into corresponding datatype ,but the thing is the string should be parse-able.

Suppose String str1=”25”;

We want to convert the String str1 to integer .

Int a=Integer.parseInt(str1);

Now the variable ‘a’ contains the value 25 which is of Integer type.

Like this we have Double.parseDouble(); etc.

o Anatomy of a Class: understand the concept of a class, define its instance variables,

constructor and methods.

Class: A class is group name to which a group of objects belong. (OR) a class is model or plan for creating objects.

A class contains properties and actions which means a class contains variables and methods.

Class doesn’t exist physically but object exists physically.

Instance variables: When we create an object a copy of variables is created in that object so it is called instance variables.

But instance variables are not available to static methods because static methods are executed prior to the creation of object.

Constructor:

It is similar to the method that is used to initialize the instance variables.The main purpose of constructor is to initialize the instance variables.

We have to follow some rules while writing a constructor.

  1. A constructor and a class name should be same.
  2. A constructor may have parameters or may not have parameters.
  3. A constructor does not return any value,not even void.
  4. Do not write anything before constructor.
  5. A constructor is called and executed automatically at the time of creating the object.
  6. A constructor is called only once at the time of creating an object.

Methods: Which contains a group of statements to perform some task or activity.

Encapsulation: understand visibility modifiers, write accessors and mutators, write the

toString method.

Encapsulation: holding the variables and the methods which acts on those variables as a single unit is called Encapsulation.

The best example for this is a Class.

Which contains instance variables ,methods which acts on those variables.

Its always Recommended to write the instance variables as private.As the scope of the private access specifier is limited to that class in which they have been declared.we can’t access them from outside the class.So we have to accessor and mutator methods.

Accessor methods area used to just to get the values of the instance variables.They will not modify the contents of the instance variables.

If we want to modify the contents of the instance variables we have to use mutator methods.

There are some access modifiers in java

Private,public,protected,default.

When a method or variable is declared as public ,it means all other classes regardless of package they belong to can access themember.

Private members :Members are marked as private cant be accessed by code in any classotehr than the class in which the private members was declared.

Protected and default members:

The protected and default members are almost identical ,but with one critical difference .

A default member may be accessed only if the class accessing the members belongs to the same package ,where as a protected member can be accessed by a sub class even if the sub class is in a different package.

toString method is used to display the contents of an object inside it.if we provided the toString()method then it checks whether the toString() is available in its super class.If is is available it will be executed,there also if is was not there then ultimate super class(Obejct) toString() will be called and executed.

_______________Thank You

Add a comment
Know the answer?
Add Answer to:
Looking for some quick and useful notes on this: • Using Classes and Objects o Creating...
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
  • JAVA :The following are descriptions of classes that you will create. Think of these as service...

    JAVA :The following are descriptions of classes that you will create. Think of these as service providers. They provide a service to who ever want to use them. You will also write a TestClass with a main() method that fully exercises the classes that you create. To exercise a class, make some instances of the class, and call the methods of the class using these objects. Paste in the output from the test program that demonstrates your classes’ functionality. Testing...

  • [JAVA] Program: Design a Ship class that the following members: A field for the name of...

    [JAVA] Program: Design a Ship class that the following members: A field for the name of the ship (a string) o A field for the year the the ship was built (a string) o A constructor and appropriate accessors and mutators A toString method that displays the ship's name and the year it was built Design a CruiseShip class that extends the Ship class. The CruiseShip class should have the following members: A field for the maximum number of passengers...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Write a class Store which includes the attributes: store name, city. Write another class encapsulating an...

    Write a class Store which includes the attributes: store name, city. Write another class encapsulating an Art Gallery, which inherits from Store. An Art Gallery has the following additional attributes: how many paintings are sold every year and the number of artists submitting artwork. Code the constructor, accessors, mutators, toString and equals method of the super class Store. Code the constructor, accessors and mutators for the subclass Art Gallery. In the Art Gallery class, also code a method returning the...

  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • You are given a specification for some Java classes as follows.   A building has a number...

    You are given a specification for some Java classes as follows.   A building has a number of floors, and a number of windows. A house is a building. A garage is a building. (This isn’t Florida-like … it’s a detached garage.) A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space,...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Tip Calculator App & Order Using NetBeans create a new JavaFX project with two classes TipCalculator...

    Tip Calculator App & Order Using NetBeans create a new JavaFX project with two classes TipCalculator (the JavaFX class) and Order Create the Tip Calculator as follows: Start with an Order class with two instance variables, item a String and price a double; include two constructors, a no-parameter constructor that passes default values empty String and zero (0) to the second constructor which then calls the set methods for item and price; the set method for price validates that price...

  • Objectives Make the transition to object oriented programming. Go over some of the basics concepts: Classes...

    Objectives Make the transition to object oriented programming. Go over some of the basics concepts: Classes Instance variables Methods Object state The keyword "this", what it does toString method Introduction Phase 1 Review the following Java programs as an example of a Java class and client code that uses it: MimicOct.java and UnderTheSea.java. All of you have been working with classes in Java. One of the most important things to know about classes is that they are actually blueprints for objects. They basically...

  • Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will...

    Using C++, this assignment uses two classes which utilize the concept of aggregation. One class will be called TimeOff. This class makes use of another class called NumDays. Then you will create a driver (main) that will handle the personnel records for a company and generate a report. TimeOff class will keep track of an employee’s sick leave, vacation, and unpaid time off. It will have appropriate constructors and member functions for storing and retrieving data in any of the...

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