SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.

Slides:



Advertisements
Similar presentations
Basic Java Constructs and Data Types – Nuts and Bolts
Advertisements

Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Warm-up: Tuesday, March 11  In programming, what is the difference between an object and a class?
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Introduction to arrays Data in economy size packages.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Datalogi A 3: 26/9. Java Concepts chapter 4 Fundamental Data Types int (long and short) double (and float) boolean char String.
More About Objects and Methods
Pemrograman Dasar - Data Types1 THINGS & STUFF Identifier, Variable, Constant, Data type, operator, expression, assignment, javadoc.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Constants, Objects, Strings Lecture 4, Tue Jan
Computer Science A 2: 6/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Fundamental data types Horstmann Chapter 4. Constants Variables change, constants don't final = ; final double PI = ; … areaOfCircle = radius *
JAVA LIBRARY CLASSES CITS Main concepts to be covered Using library classes: String, Math, Color Reading documentation Java 7 API is available.
1 Programming Section 11 James King 12 August 2003.
Unit 3: Java Data Types Math class and String class.
Copyright © Curt Hill Mathematics Functions An example of static methods.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Static Methods and Static Variables.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
ROUND 1 Name a method associated with class String 1.) 15 compareTo() 26 indexOf() 34 length() 2.) 3.) 4.) 3 toUpper() 7 substring() 11 charAt() 5.)
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
CS-1030 Dr. Mark L. Hornick 1 C++ Language Basic control statements and data types.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Introduction to Programming
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Lecture 10:Static & Final Kenya © 2005 MIT-Africa Internet Technology Initiative MyMath Example public class MyMath { public double PI = ;
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
MIT AITI 2004 Lecture 10 Static and Final. public class MyMath { public double PI = ; public double square (double x) { return x * x; } public.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
A: A: double “4” A: “34” 4.
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Computer Science 112 Fundamentals of Programming II.
Libraries and APIs Don’t reinvent the wheel. What’s an API? API – Application Programmers Interface –We want to use code someone else has written –API’s.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
More on Arrays Review of Arrays of ints, doubles, chars
Math class Random() method Floor method Top-level parseInt function
More Sophisticated Behavior
OUTPUT STATEMENTS GC 201.
Lecture 4 Using Classes Richard Gesick.
Static and non-Static Chapter 5.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Defining Your Own Classes Part 1
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 6 Methods: A Deeper Look
Introduction to Java Programming
Java Tutotrial for [NLP-AI] 2
Arrays and Collections
CSE 1341 Topic 5 Methods © 2013 Ken Howard,
Variables, Types, Operations on Numbers
Using java libraries CGS3416 spring 2019.
String Class.
More on iterations using
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS

Java’s pre-built classes are stored in the API class libraries ~200 packages 1000’s of classes 10,000’s of methods Winter 2004CS-1010 Dr. Mark L. Hornick 2 Moral: Don’t reinvent the wheel

To use a method, you need to know how to interface to it For example, to round a value to the nearest whole number, use the round method of the Math class: public static long round(double a) This is the method signature as it appears in the API documentation round – the name of the method public – tells us we can call it static – we call the method via the class long – it returns a long datatype as a result double – the datatype of the argument (value) we supply a – the method’s internal name for the value we supply CS-1010 Dr. Mark L. Hornick 3

The call to a method must conform to the signature The round method of the Math class’s signature: public static long round(double a) Suppose we have a variable x containing some value. We make a call to Math.round using the following syntax: long roundedValue = Math.round( x ); or long y = Math.round( 7.0/3 ); Internally, round uses ‘a’ to hold our argument’s value; we can use any identifier name or double expression as the supplied value CS-1010 Dr. Mark L. Hornick 4

The Math class contains many static methods and a few constants: Math.PI – the ratio of the circumference of a circle to its diameter Math.E – the base of the natural logarithms Winter 2004CS-1010 Dr. Mark L. Hornick 5

SE-1010 Dr. Mark L. Hornick 6 The String datatype is a built-in class that represents a sequence of characters “a” “SE-1010” “123” “This is a 6 word string.” “” There is no fundamental restriction on the maximum length of a Java String How does this differ from a int that represents 123? This represents an empty String

Methods of the String class are both static and non-static A static method: static String format( String format, Object… args) double area = ; String s = String.format(“Area is %8.3f”, area); // s contains “Area is 6.123” A non-static method: char charAt( int index ) String s = “Hello”; char c = s.charAt(1); // c contains ‘e’ Winter 2004CS-1010 Dr. Mark L. Hornick 7

Non-static methods of a class are called via a class instance Here, s is an instance of a String: String s = “Hello”; char c = s.charAt(1); // c contains ‘e’ Winter 2004CS-1010 Dr. Mark L. Hornick 8