Netbeans tutorial:

Similar documents
Fundamentals of Programming Data Types & Methods

- Thus there is a String class (a large class)

CS212 Midterm. 1. Read the following code fragments and answer the questions.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

STUDENT LESSON A10 The String Class

Activity 9: Object-Oriented

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

TEST (MODULE:- 1 and 2)

Task #1 The if Statement, Comparing Strings, and Flags

Basic Computation. Chapter 2

Section 2.2 Your First Program in Java: Printing a Line of Text

Computer Science 145 Midterm 1 Fall 2016

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

LAB 12: ARRAYS (ONE DIMINSION)

Here is how we use an arraylist. To access the arraylist code we can import the class via:

Example Program. public class ComputeArea {

Oct Decision Structures cont d

Activity 9: Object-Oriented

Programming with Java

Arrays. Eng. Mohammed Abdualal

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Lab 3: Call to Order CSCI 2101 Fall 2018

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Lab 3: Call to Order CSCI 2101 Fall 2017

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Chapter 6 Lab Classes and Objects

St. Edmund Preparatory High School Brooklyn, NY

In this introduction to UNIX, only the dumb terminal (also known as the command line ) interface is discussed.

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

Eng. Mohammed Abdualal

More on variables and methods

Loops. CSE 114, Computer Science 1 Stony Brook University

Java Programming. String Processing. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Using Java Classes Fall 2018 Margaret Reid-Miller

H212 Introduction to Software Systems Honors

CSIS 10A Assignment 9 Solutions

LAB 13: ARRAYS (ONE DIMINSION)

Chapter 3 Lab Decision Structures

Section 2.2 Your First Program in Java: Printing a Line of Text

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

String. Other languages that implement strings as character arrays

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Manual for Basic Java

Full file at

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSE 1223: Exam II Autumn 2016

CSE1710. Big Picture. Tuesday, Dec 02 is designated as a study day. No classes. Thursday, Dec 04 is last lecture.

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Over and Over Again GEEN163

Chapter 9 Lab Text Processing and Wrapper Classes

CSE 114 Computer Science I

appreciate the difference between a char and a string understand and use the String class methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Chapter 6 Lab Classes and Objects

Midterm Examination (MTA)

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lab #7 Library Classes and JUnit Testing. Daniel Amyot, Diana Inkpen, Alan. Agenda. In this lab, you are going to create your own

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Practice Midterm 1. Problem Points Score TOTAL 50

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Chapter 1 Lab Algorithms, Errors, and Testing

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

2.8. Decision Making: Equality and Relational Operators

Mr. Monroe s Guide to Mastering Java Syntax

Computer Applications Answer Key Class IX December 2017

Decisions in Java IF Statements

CS 180. Recitation 8 / {30, 31} / 2007

CS 101 Fall 2006 Midterm 1 Name: ID:

Full file at

coe318 Lab 1 Introduction to Netbeans and Java

COMP-202 Unit 5: Basics of Using Objects

Strings! Today! CSE String Methods!

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Welcome to the Using Objects lab!

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

Basic Computation. Chapter 2

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Chapter 4: Control Structures I

How to define your own classes that implement abstractions. How to pass information to methods and how methods return values.

Software and Programming 1

Chapter 8: Strings and Things

Unit 4: Classes and Objects Notes

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

Object Oriented Programming. Java-Lecture 1

Chapter 10: Text Processing and More about Wrapper Classes

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Transcription:

COE808 Lab2 Prelab preparation Before coming to the lab you should: 1. Read the lab. The most recent version can be found at the URL: www.ee.ryerson.ca/~courses/coe808 2. Try to prepare any questions you may have about the lab. To get started you must perform the following operations. 1. Create your lab2 sub-directory with the command: mkdir lab2 2. Change to your lab2 directory with the command: cd lab2 In this lab you must use Netbeans as IDE Netbeans tutorial: http://netbeans.org/kb/docs/java/quickstart.html Exercise 1 Goals for this exercise: 1. Explore some of the properties of the pre-defined class. 2. Read lines of keyboard input with a Scanner object. s: s are represented as objects of the class, which is defined in the java.lang package. s can be created in several ways, of which we consider two: name = "Jean Valjean"; or by using a constructor in the class that takes a string as a parameter and creates a unique object with the same value. For example: protagonist = new (name); Now, both variables name and protagonist are references to objects which have the same value, "Jean Valjean". In this exercise, we wish to explore the common, very useful instance methods described in the table below. The object to which the method is being applied is referred to as this in the descriptions: 1

METHOD DESCRIPTION EXAMPLE Boolean equals( string2 ) int length( ) char charat(int position) int indexof( string2) substring(int start) substring(int start, int end) touppercase() tolowercase() returns true or false depending upon whether this and the parameter have the same value returns the length of this object returns the character (type char) in this at index position. Position is a non-negative integer less than the length of this. returns the index of the first occurrence of the string string2 in this object. Returns -1 if string2 not found. returns the substring of this object starting from start through to the end of the object. returns the substring of this object starting from start through, but not including, index end of this object. return this converted to upper case. return this converted to lower case. x = "123", y= "12" + "3"; x.equals(y) returns the value true. greeting = "Bonjour"; greeting.length( ) returns the value 7. greeting = "Bonjour"; greeting.charat(5) returns the value 'u' greeting = "Bonjour", word = "jou"; greeting.indexof(word) returns 3. name = "Sheryl Crow"; name.substring(7) returns the string "Crow" name = "Sheryl Crow"; name.substring(3,8) returns the string "ryl C" "Hi Jane!".toUpperCase() returns the string "HI JANE!" "Hi Jane!".toLowerCase() returns the string "hi jane!" Part I 1. In Netbeans, save your program as "Project1" on your lab2 directory. Name your new class Demo and save it to your directory. import java.util.scanner; /** * Here we will explore some of the methods of the class. * We will distinguish == and equals. * We will also use the Scanner class to read a whole line of input. */ public class Demo { public static void main ([] args){ str1 = "It was the best of times."; 2

str2 = str1; str3 = "It was the best of times."; str4 = "Of times it was the best."; System.out.println("str1=" + str1); System.out.println("str2=" + str2); System.out.println("str3=" + str3); System.out.println("str4=" + str4); System.out.println(); System.out.println("The length of str1 is " + str1.length()); System.out.println("Index of 'b' in str1 is: " + str1.indexof('b')); System.out.println("Index of 'x' in str1 is: " + str1.indexof('x')); System.out.print("The substring of str1 from "); System.out.println("position 7 to position 14 is: " + str1.substring(7,15)); System.out.println("str1 in upper case is: " + str1.touppercase()); if (str1==str2) System.out.println("str1 and str2 refer to the same object."); if (str1!=str3) System.out.println("str1 and str3 do NOT refer to the same object."); if (str1.equals(str3)) System.out.println("str1 and str3 contain the same characters."); if (!str1.equals(str4)) System.out.println("str1 and str4 do NOT contain the same characters."); Scanner in = new Scanner(System.in); System.out.println("Enter a line:"); user1 = in.nextline(); System.out.println("Enter a second line:"); user2 = in.nextline(); if (user1.equals(user2)) System.out.println("Your strings are the same."); // An object returned can be immediately used to call another method // or be used as a parameter: else if (user1.touppercase().equals(user2.touppercase())) System.out.println(" If you ignore case, your strings are the same."); else System.out.println("Your strings are different."); System.out.println("Done"); // end of main method // end of class Compile and run the main program, type the two lines of input requested into the terminal window (remembering to press the Enter key both times), and analyze the output. Try to understand the effect of each method call. Part II Design, compile and run Java programs to accomplish each of the following tasks. The program can just include a main method like the program in part I. In fact you can modify the program in part I if you like. Each part can be in a separate method or you can put it all in the main program. Be sure to include prompts for each line of input requested: (A) Read a sentence (string) from a line of input, and print whether it represents a declarative sentence (i.e. ending in a period), interrogatory sentence (ending in a 3

question mark), or an exclamation (ending in exclamation point) or is not a sentence (anything else). (B) Read a name from a line of input (first and last separated by a space) and print last name first followed by a comma followed by the first name. For example, if the input is "Marcel Proust", the output is "Proust, Marcel". (C) Read a string and print the characters in reverse order. For example, if the input string is "Mine is a sad tale, said the mouse.", the output string would be ".esuom eht dias,elat das a si enim". Exercise 2 In Netbean, save your program as "Project2" on your lab2 directory. Create new classes. Save these classes to your directory and study it to see how it works. Consider the following hierarchy of classes, with various details to be filled in: // Person.java: abstract base class public abstract class Person { public abstract double getwages(); // abstract, so omit body // Employee.java: the class Employee public class Employee extends Person { protected name; // in the form "Last, First" protected address; // constructor public Employee( n, a) { // first name, then address // Fill in for Part a. // convert the point into a representation public to() { return "[" + name + ", " + address + "]"; public double getwages() { // must be either faculty or staff for wages return 0.0; // Faculty.java: the class Faculty public class Faculty extends Employee { // inherits from Employee 4

protected double salary; // Constructor public Faculty(double s, n, a) { // salary, name, address // Fill in for Part b. // convert the Faculty class to a public to() { // Fill in for Part c. public double getwages() { return salary; // Staff.java: the class Staff public class Staff // Fill in for Parts d., e., f. // Persons.java: test the Person-Employee-Faculty-Staff hierarchy public class Persons { public static void main ([] args) { Person[] persons = new Person[3]; persons[0] = // Fill in for Part g. persons[1] = // Fill in for Part g. persons[2] = // Fill in for Part g. printpersons(persons); System.out.println(); public static void printpersons(person[] s) { for (int i = 0; i < s.length; i++) System.out.println(s[i]); a. Fill in the constructor for Employee above. b. Fill in the constructor for Faculty above, using the Employee constructor with super. 5

Exercise 3 c. Fill in the to method for Faculty above, using the Employee to with super. d. Write a Staff class that inherits from Employee (that is, extends Employee). The Staff class should have data fields double hourlyrate and int hoursworked. getwages method of the Staff class will return hourlyrate*hoursworked. e. Write a constructor for Staff that takes parameters the two new ones above, followed by the name and address. f. Write a to method for Staff above, using the Employee to with super. Consider the grammar g. In the Persons class above, create new objects of each of the three kinds: Employee (with name "Nobody, Nonce", and address "120 Worry Lane") Faculty (with salary 2020, name "Blow, Joe", and address "222 Center St.") Staff (with hourly rate 7.5, hours worked 40, name "Bonkers, Bruce", and address "1 Prime Ave"). <P> <S>$ <S> b<m>b <M> (<L> <M> a <L> <M> a) (P is the start symbol) Design a recursive descent parser for this grammar. In Netbeans, save your program as "Project3" on your lab2 directory. The parser class must be named as GrammarTest. The example sentence to be parsed is b ( ( a a ) a ) b $ Submitting your lab (1) Zip your lab2 assignment within your coe808 directory zip r lab2 lab2 (2) Attach your lab2.zip file and send e-mail to GA, Mr. Lei Gao, subject of your e-mail must be lab1 followed by your name and student id. e-mail: lei.gao@ryerson.ca 6