Download presentation
Presentation is loading. Please wait.
Published byJanis Tucker Modified over 9 years ago
1
TCU CoSc 10403 Introduction to Programming (with Java) Getting to Know Java
2
Problem Solving The purpose of writing a program is to solve a problem. Problem solving consists of multiple steps: Understanding the problem. Breaking the problem into manageable pieces. Designing a solution. Implementing the solution. Testing the solution and fixing any problems that exist.
3
The Java Programming Language A programming language defines a set of rules: –That determine exactly how a programmer can combine words and symbols of the language into programming statements. Programming statements are the instructions that are carried out when the program is executed. –Java is an object-oriented programming language (more about that later). Java is one of the fastest growing programming technologies of all time.
4
Reasons Why Java is Popular Java is the first programming language to deliberately embrace the concept of writing programs that can be executed on the Web. Moreso, Java is a useful general purpose programming language. The Java language is accompanied by a library of extra software that we can use when developing programs. –The library provides the ability to create graphics, communicate over networks, and interact with databases. –The set of supporting libraries is huge and versatile.
5
I’ve been telling you that Java is an Object Oriented Programming Language - what exactly does that mean? To understand - we need to discuss and understand what an Object is and how it relates to a Class!!
6
Anatomy of a Java class A class is a description of a template (or pattern) that specifies the types of data values that an instance of the class can hold and the operations that it can perform. In OOP, rather than describe each and every object, the programmer describes the general type of an object (a class) and then uses the class to create (or instantiate) as many of the objects as are needed. Example: a class describing”generic” students at TCU. Instance variables - the data that is maintained for students (ex., name, address, age, …) Constructors - methods that are involved in the construction of an instance of a class. Methods - subprograms that permit instance variables to be accessed and modified (ex., setName(), setMajor(), getAge(), setAge(), … )
7
Example: consider a banking problem Bank Account A class (the concept) John’s Bank Account Balance: $5,257 An object (the realization) Bill’s Bank Account Balance: $1,245,069 Mary’s Bank Account Balance: $16,833 Multiple objects from the same class
8
Let’s discuss these concepts further!! A class -- the basic building block of an object-oriented language. It is a template/pattern that describes the data and behavior associated with instances of that class. When you instantiate a class you create an object that looks and feels like other instances of the same class. –The data associated with a class or object is stored in instance variables; –the behavior associated with a class or object is implemented with methods. Methods are similar to the functions or procedures in other procedural languages. **Some author wrote: Julia Child's recipe for rack of lamb is a real-world example of a class. Her rendition of the rack of lamb is one instance of the recipe, and mine is quite another. (While both racks of lamb may "look and feel" the same, I imagine that they "smell and taste" different.)
9
Constructors A constructor: – is a special method that is used to set up a newly created object – often sets the initial values of variables (i.e., it can be passed information that can be used to initialize some variables in the object) – has the same name as the class –does not return a value – has no return type, not even void The programmer does not have to define a constructor for a class (there is a default constructor for every class – its syntax is: theNameOfTheclass() For example, a class named Student could have a constructor that receives a parameter specifying the Student’s name: Student tommy = new Student(“Tom Jones”);
10
Inheritance Classes are defined in a hierarchy A class can extend (inherit from) some other class Object => Component => Container => Panel => Applet Thus, an Applet can do everything that Panel does and some other things. A Panel can do everything a Container can do, etc… Advantage –Easy to extend or specialize something that already exists. Note: we take advantage of this when we write: public class HelloWorld extends Applet { … } Disadvantage –Sometimes difficult to find everything that a class does; you may have to look up the tree.
11
Inheritance One class can be used to derive another via inheritance. A class can extend (inherit from) some other class Administrator class(extends Person) Faculty class(extends Person)Student class (extends Person) Person class (called a superclass) Subclasses or “derived” classes - may have more than just 2 levels.
12
Java Components Package (what the import statement brings in!!) –collection of related classes Class –description of an object (template) –variables + methods Variable – name for memory location for a data item Method – specifies an action on an object –retrieve/return info or change info Object –instance of a class –created by the “new” operator Java program = collection of cooperating objects
13
Creating Objects The new operator creates a new object from a class: Example: Person jackSmith = new Person (); Person sallyJones = new Person(); This declaration asserts that jackSmith and sallyJones are variables that refer to objects created from the Person class. They are initialized (set to point) to the objects created by the new operator The newly created object is actually created and allocated to memory by a call to a very special method contained within the class - a constructor of the class. Instance of the Person class containing a copy of all instance variables and all methods jackSmith Instance of the Person class containing a copy of all instance variables and all methods sallyJones
14
Choosing Identifier Names Identifier names should be descriptive. Avoid meaningless names such as a or x. Unless the name is actually descriptive, such as using x and y to represent (x, y) coordinates. –Avoid using unnecessarily long names (unless they promote better readability - example: yearToDateSalary). A “name” in Java is a series of identifiers separate by the dot (period) character. –The name System.out is the ‘name’ of an object through which we invoke the println method. (println is a method inside the object.)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.