The method main in our example has only one action

Info icon This preview shows pages 4–7. Sign up to view the full content.

View Full Document Right Arrow Icon
The method main() in our example has only one action statement: System.out.println(”Hello World!”); CPEN 221 – Fall 2016
Image of page 4

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
An Introduction to Java 5 This is the statement that prints “Hello World!” to the standard output device. The . (dot) operator is used in Java to access “stuff” within an object (or a class – in some circumstances). When we write System.out.println(”Hello World!”); , what we are stating is that the Java runtime environment (the JVM) should execute the method println() that belongs to the object out , and that out itself belongs to the object/class System . For those familiar with C, the equivalent program might look like this: #include <stdio.h> // print the string ”Hello World!” int main( int argc, char * argv[]) { printf( ”Hello World!\n” ); } The C program appears more compact (fewer lines of code) than HelloWorld.java . Java can be a verbose language at times. Python is even more compact because we only need to write the following: print ( ”Hello World!” ) 5 Of Classes and Objects In the previous example, we looked at a trivial Java program that did not use objects in any significant way. We will now delve a bit into the need for objects and how to use them (at an introductory level) in Java. Classes and objects are Java’s approach to creating user-defined data types. (Other languages may also permit the creation of user-defined types, but without the use of objects.) 6 What are data types? Data types are simply a set of values and the operations that are permitted on the val- ues. CPEN 221 – Fall 2016
Image of page 5
An Introduction to Java 6 Java, like C, supports primitive types such as int , float , double and char . (This is not an exhaustive list.) int is the data type that represents the set of integer values that can be represented on a computing platform and some of the standard arithmetic operations (such as addition, subtraction, multiplication, division) are defined on the int s. Let us suppose we wanted to step up the level of abstraction and deal with a set of circles. (This is a rather common situation if you were developing a graphics package or a video game.) To represent a circle, we may need to specify its centre ( x, y coordinates) and its radius ( r ). If we wanted to work with 50 circles then we could, possibly, use an array of x values, an array of y values and an array of r values with the convention that x[i], y[i], r[i] represent circle i . Such an approach may work but it may also be difficult to keep track of all the arrays and indices. We may want to have a datatype called Circle that encapsulates the center coordinates as well as the radius, and has some well-defined operations that are relevant to a circle (find the area of the circle, find the circumference of the circle). Such a datatype can be created in Java through its classes and objects. We can declare a public class called Circle as follows (naturally in the file Cir- cle.java ): public class Circle { public double x, y; // these represent the center coordinates public double r; // this represents the radius of the circle /** * This is the default constructor for the class Circle.
Image of page 6

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern