The need for Programming Languages

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

CSCI 160 Midterm Review Rasanjalee DM.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
Outline Java program structure Basic program elements
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
Introducing Java.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
(C) 2010 Pearson Education, Inc. All rights reserved.  Java programs normally go through five phases  edit  compile  load  verify  execute.
Topic 3 The Stack ADT.
Java Language and SW Dev’t
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Computer Systems and the Java Programming Language.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Working with arrays (we will use an array of double as example)
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Review, Pseudocode, Flow Charting, and Storyboarding.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
By Mr. Muhammad Pervez Akhtar
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Information and Computer Sciences University of Hawaii, Manoa
More About Objects and Methods
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Object Oriented Programming
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
Yanal Alahmad Java Workshop Yanal Alahmad
Testing and Debugging.
Internet and Java Foundations, Programming and Practice
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Statements, Comments & Simple Arithmetic
An Introduction to Java – Part I, language basics
slides created by Ethan Apter
Chapter 1: Computer Systems
slides created by Ethan Apter
Focus of the Course Object-Oriented Software Development
Introduction to Data Structure
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Classes, Objects and Methods
slides created by Ethan Apter and Marty Stepp
Classes and Objects Object Creation
Corresponds with Chapter 5
SPL – PS1 Introduction to C++.
CS 240 – Advanced Programming Concepts
CMSC 202 Constructors Version 9/10.
Presentation transcript:

The need for Programming Languages Computers are very simple devices in that they only understand a handful of simple commands, like adding two numbers or reading a value from memory. The set of commands understood by a computer is called machine code. The processor is the component of a computer that executes the commands in a program. Each processor has its own machine code. A program needs to be stored in the memory of the computer, so it can be executed. Information is stored in a computer in binary format, i.e, information is encoded as a sequence of 0’s and 1’s.

Below is a binary program in machine code for an old processor called 8086. This program prints the word “hello” on the screen. 1011101000001100000000011011010000001001110011010010000110111000000000000100110011001101001000010100100001100101011011000110110001101111001011000010000001010111101101111011100100110110001100100000100001000011010000110001101111 Binary code is hard for humans to understand, as a sequence of 0’s and 1’s has no meaning to us.

Compare the above binary program with the following equivalent python program print (“hello”) Python is called a high level programming language and it was designed to make computer programs readable to humans. However, a computer does not understand python, java, C++, or any other high level programming language. A compiler is a piece of software that translates programming language into code that the computer can understand.

In this course we will be writing programs in java In this course we will be writing programs in java. A java program must be stored in a file with the extension .java. A java compiler does not directly produce machine code, but it translates the java program into another language called java bytecode. Java bytecode is a kind of intermediate language. Java bytecode is stored in files with the extension .class. A java interpreter or virtual machine can execute the java bytecode. Eclipse has an integrated java compiler that runs as you type your program. If you want to compile your java program for a terminal or command window, the name of the java compiler is javac and the name of the java interpreter is java.

The following two classes are used to illustrate the notions of instance variables and public and private methods. Class Person.java represents objects, each one of which stores the first name, last name, and email address of an individual. Setter and getter methods are used to access the private information stored in an object of this class. Class SocialNetwork.java has an array instance variable called listFriends which can store a set of objects of the class Person. Method add allows a new Person object to added to the array. Notice that the size of the array is increased as more data items

are stored in it. Method expandCapacity() doubles the size of the array and copies the information from the smaller array to the larger one.

/* Constructor initializes the person's name and public class Person { /* Attribute declarations */ private String lastName; // last name private String firstName; // first name private String email; // email address public Person() { /* Constructor */ lastName = “”; firstName = “”; email = “”; } /* Constructor initializes the person's name and email address */ public Person(String first, String last, String mail) { firstName = first; lastName = last; email = mail; }

public String getName() { return firstName + " " + lastName; } public String getEmail() { return email; public void setEmail (String mail) { email = mail; public void setName(String newFirst, String newLast) { firstName = newFirst; lastName = newLast; public String toString() { String s = firstName + " " + lastName + "\t" + email; return s; }

* equals method determines whether two Person objects /** * equals method determines whether two Person objects * have the same name * @param other: Person object that this is compared to * @return true of they have the same first name and last * name, false otherwise */ public boolean equals(Person other){ if (this.firstName.equals(other.firstName) && this.lastName.equals(other.lastName)) return true; else return false; }

public class SocialNetwork { // default size of array private final int DEFAULT_MAX_FRIENDS = 10; /* Attribute declarations */ private Person[] friendList; // list of friends private int numFriends; // current number of // persons in list /** * Constructor creates person array of default size */ public SocialNetwork () { friendList = new Person[DEFAULT_MAX_FRIENDS]; numFriends = 0; }

/** * Constructor creates person array of specified size * @param max maximum size of array */ public SocialNetwork(int max) { friendList = new Person[max]; numFriends = 0; } public void add (String firstName, String lastName, String email) { Person friend = new Person(firstName, lastName, email); // if array is full, increase its capacity if (numFriends == friendList.length) expandCapacity(); // add new friend at first free entry in array friendList[numFriends] = friend; numFriends++;

/** * expandCapacity method is a helper method * that creates a new array to store friends with twice the * Capacity of the existing one */ private void expandCapacity() { Person[] largerList = new Person[friendList.length * 2]; for (int i = 0; i < friendList.length; i++) largerList[i] = friendList[i]; friendList = largerList; } public String toString() { String s = ""; for (int i = 0; i < numFriends; i++) { s = s + friendList[i].toString()+ "\n"; } return s;

Class SocialNetwork contains a method for removing a data item from the array. To remove a data item, say target, from the array we first need to find the position of such an item in the array. A simple way of looking for target in array friendList is to take the data items stored in the array one by one starting at the data item stored in index 0 and compare each one of them with target until either target is found, or all data items have been examined and target is not found The above algorithm for looking for a data item in a list is called linear search. Once item target has been found in the array we can remove it by replacing it with the last item in the array. Pseudocode for removing a data item from the array follows.

Algorithm remove(target) Input: data item to be removed Output: true if target was removed from the array; false if target was not found in the array i = 0 while (i < numFriends) and (friendList[i] not equal target) do i = i+1 if i = numFriends then return false else { friendList[i] = friendList[numFriends-1] friendList[numFriends-1] = null numFriends = numFriends -1 return true }

The advantage of writing an algorithm in pseudocode is that we can concentrate on designing the steps that the algorithm needs to perform to achieve the desired task without having to think about how to express the algorithm in correct java syntax. Once we have designed a correct algorithm for a problem in pseudocode, translating it into java is a somewhat mechanical process. Writing algorithms in pseudocode and then translating them into java makes it easier to design programs.

The beauty of pseudocode is that there is no fixed syntax or rigid rules for it. Pseudocode is a mixture of English and programming-like statements. Each programmer comes up with their own version of pseudocode. The programmer just needs to ensure that pseudocode is understandable to other people and that it is detailed enough that translation into java or other programming language is simple. There should be an (almost) one-to-one correspondence between lines of pseudocode and lines of java. The java version for the remove algorithm follows.

public boolean remove(String firstName, String lastName) { Person target = new Person(firstName, lastName, ""); // search the list for the specified friend int i = 0; while ((i < numFriends) && !friendList[i].equals(target)) i++; if (i == numFriends) return false; else { // person found, remove by replacing with last one friendList[i] = friendList[numFriends - 1]; friendList[numFriends - 1] = null; numFriends --; return true; }

public class MyFriends { public static void main(String[] args) { SocialNetwork contacts = new SocialNetwork(); contacts.add("Snoopy","Dog","snoopy@uwo.ca"); contacts.add("Felix","Cat","felix@uwo.ca"); contacts.add("Mickey","Mouse","mickey@uwo.ca"); System.out.println(contacts.toString()); }

The second line in the above class: public static void main (String[] args) { states that method main, when invoked can receive any number of arguments. Java will create an array of the correct size to store all the arguments. Java was designed so that the first method that is executed in any java program is main. The arguments that are passed to this method are called the program arguments or command line arguments. The keyword static tells the java compiler that when the program is executed a special object, sometimes called a static object, needs to be created. Static objects are different from other objects in that they are not created by the programmer with the new statement. Since to run a java program objects need to be created first, static objects solve the issue of how to create the very first object of a program.