Chapter One Notes from text and class
Chapter One Notes from text and class CSC 120
Popular in INTRODUCTION TO COMPUTER PROGRAM
Popular in ComputerScienence
This 5 page Class Notes was uploaded by AnnMarie on Monday December 7, 2015. The Class Notes belongs to CSC 120 at Louisiana Tech University taught by Debra Blackman in Winter 2015. Since its upload, it has received 48 views. For similar materials see INTRODUCTION TO COMPUTER PROGRAM in ComputerScienence at Louisiana Tech University.
Reviews for Chapter One Notes from text and class
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 12/07/15
Chapter 1 Objects and Classes An object is an abstraction of a set of real world things such that all the realworld things in the set the instance have the same characteristics all the instances have the same behaviors In class Blackman suggested that we should use a large dounut as an typlate when creating objects because the hole center will be the attributes that make up the object and the outside cake part makes up the actions or behaviors that the object can do Objects are created from classesThe class describes the kind of object the objects represent instantiations of the class Objects are refered as instances We use naming conventions while programing to make it easier to distinguash between classes and objects A class will have the first letter uppercase such as Animals An object will have the first letter lowercase and any additional words will begin with uppercase such as whiteLion In Blue J using the figures example we created an instance object by creating a new Circle by right clicking on the Circle Class and choosing new Circle We left the automatic name for the new instance object alone and clicked Ok We noticed that at the bottom of the screen there was a red rectangle that had circle1 Circle this area is called the object bench and it will show all instances objects that were created When you rightclick on the circle instance an operations menu If you click on the void makeVisable the circle instance will appear in a separate window These operations are called methods which in turn are called or invoked You can move the circle instance with any of the move operations such like void moveRight and a dialog will appear prompting fo an input This prompt is asking for the parameter which can be anything from a number to text This provides additional information to the method If you look at the list of methods for the circle instance they begin with void and then some name with parameters at the end This is called a singature The part enclosed by parenthese int distance is the information about the required parameter and also defines a type and a name The type defines what kinds of values a parameter can take and the name gives a hint about the meaning of the data expected For instance void moveRightint distance is looking for the data type of an int because that will be the distance that it moves right If you tried to change the color of circle1 you may have notice that it gives an error if you type in blue This happened because it was looking for a character type and you have to put around the text If you look at Appendix B in the textbook you will find a table that describes the different data types that exist in Java On the next page you will notice that I included that table in these notes Type Name Description Example literals Integer Numbers byte bytesized integer 8 bit 24 2 short short integer 16 bit 137 119 int integer 32 bit 5409 2003 long long integer 64 bit 423266353L 55L Real Numbers oat singleprecision oating point 43889F double doubleprecision floating point 4563 24e5 Other Types char a single character 16 bit 39m39 3939 39u00F6 boolean a boolean value true or false True False Once you have a class you can create multiple instances of that class and each instance has its own attributes So if you make changes to circle1 it will not affect circle2 These attributes define an instance39s state represented by storing value in fields In BlueJ the state of an instance can be inspected by selecting the Inspect function from the instance39s popup menue The object inspector window will display the details of the object Some methods when called change the state of an instance Java referes instance attributes as fields When multiple instances are created the instances of the same class have the same fields This is because the class determines how many fields will the instance have and also defines each instance39s field types This is similar for methods also Methods are defined in the class of the instances and all instances of a given class have the same methods When programing in Java we write instructions down to invoke methods on instances Taking a look at the following code we can determine the following Person person1 new Person person1makeVisible person1moveRight First a new person instance was created from the Person class and stored to person1 as the variable name Second person1 was made visible by changing the field isVisible to true by invoking the makeVisible method on the person1 instance Third the instance was move to the right by invoking the moveRight method In BlueJ you can use the Show Terminal from the View menu to see the text output when methods are called You will need to select the Record method calls from the terminal39s options menu to see the text To be able to invoke methods in Blue you will need to open the Show Code Pad from the view menu This allows you to type Java code Note if you leave out the after a method call you will recieve an error The Blue project house shows that instances can create other instances by calling each other39s methods To look at the source code for the house project rightclick on the Picture class and select Open Editor If you make any changes to the source code you need to recompile the class This can be done by clicking on the Compile button The BueJ project labclasses is used to explain that methods may return values and that objects can be passed as parameters to other methods The value that results from a return is called the result value If you look at the code below you will see that the word void is not infront This is because void does not return a value public String getName return name When invoking the enrollStudent method you may notice that it takes the instance student1 student2 ect This is passing the object as a parameter to the enrollStudent method for the LabClass class Keywords that you may want to remember Object Java objects model objects from a problem domain Class Objects are created from classes The class describes the kind of object the objects represent individual instantiations of the class Method We can communicate with objects by invoking methods on them Parameter Methods can have parameters to provide additional information for a task Signature The header of a method is called its signature and it provodes inforamtion needed to invoke that method Type Parameters have types The type defines what kinda of values a parameter can take Multiple Instances Many similar objects can be created from a single class State Objects have state The state is represented by storing values in fields Method Calling Objects can communicate by calling each other39s methods Source Code The source code of a class determines the structure and behavior the fields and methods of each of the objects in that class Result Methods may return information about an object via a return value Chapter 1 Exercises 130 In this chapter we have mentioned the data types int and String J ave has more predefined data types Find out what they are used for Answer byte bytesized integer that stores 8 bits short short integer that stores 16 bits long long integer that stores 64 bits float singleprecision oating point double doubleprecision floating point char a single character that stores 16 bits boolean a boolean value that is true or false 131 What are the types of the following values 0 hello 101 1 true 33 31415 Answer 0 byte short int or double hello String 101 short byte int long 1 byte short int long true boolean 33 String 31415 double oat 132 What would you have to do to add a new field for example one called name to a circle object Answer In the fields section we would add Private String name 133 Write the signature for a method named send that has one parameter of type String and does not return a value Answer public void send String message 134 Write the signature method named average that has two parameters both type int and returns an int value Answer public average int a int b 135 Look at the book you are reading right now Is it an object or a class If it is a class name some objects If it is an object name its class Answer The book that I am reading is an object and the name of the class is book 136 Can an object have serveral different classes Discuss Answer Yes for instance you may have a class called Book and another class called Textbook This is an advanced concept for Java which will be taught in the next quarter