LAB4 Making Classes and Objects



Similar documents
LAB 3 Part 1 OBJECTS & CLASSES

Install Java Development Kit (JDK) 1.8

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Java Crash Course Part I

CS170 Lab 11 Abstract Data Types & Objects

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

How to use the Eclipse IDE for Java Application Development

Java CPD (I) Frans Coenen Department of Computer Science

Moving from CS 61A Scheme to CS 61B Java

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Introduction to Object-Oriented Programming

CS 106 Introduction to Computer Science I

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

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

DrJava User Documentation

Appendix A Using the Java Compiler

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Manual For Using the NetBeans IDE

Part I. Multiple Choice Questions (2 points each):

Week 2 Practical Objects and Turtles

J a v a Quiz (Unit 3, Test 0 Practice)

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Lab Experience 17. Programming Language Translation

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

AP Computer Science Static Methods, Strings, User Input

Object-Oriented Programming Lecture 2: Classes and Objects

Object-Oriented Programming in Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Part 1 Foundations of object orientation

Java Programming Fundamentals

AP Computer Science Java Mr. Clausen Program 9A, 9B

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Chapter 1 Java Program Design and Development

Sample CSE8A midterm Multiple Choice (circle one)

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Creating a Java application using Perfect Developer and the Java Develo...

Installing Java (Windows) and Writing your First Program

Java Programming Unit 1. Your first Java Program Eclipse IDE

2 Getting Started with jgrasp 2.0

With a single download, the ADT Bundle includes everything you need to begin developing apps:

1001ICT Introduction To Programming Lecture Notes

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Lecture 5: Java Fundamentals III

Object-Oriented Programming in Java

How to Install Java onto your system

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Java Classes. GEEN163 Introduction to Computer Programming

Java from a C perspective. Plan

ECE 122. Engineering Problem Solving with Java

Programming Languages CIS 443

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

University of Pennsylvania. Electrical & Systems Engineering Undergraduate Laboratories. ESE 112: Introduction to Electrical & Systems Engineering

Introduction to Java

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

CSE 308. Coding Conventions. Reference

Java is an Object-Oriented Language. As a language that has the Object Oriented feature, Java supports the following fundamental concepts:

Software Engineering Techniques

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Eclipse with Mac OSX Getting Started Selecting Your Workspace. Creating a Project.

Using Eclipse to Run Java Programs

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Crash Course in Java

Getting Started with the Internet Communications Engine

Java applets. SwIG Jing He

Chapter 3. Input and output. 3.1 The System class

JAVA - OBJECT & CLASSES

CS 170 Java Programming 1. Welcome to CS 170. All about CS 170 The CS 170 Online Materials Java Mechanics: Your First Program

Using Files as Input/Output in Java 5.0 Applications

10 Java API, Exceptions, and Collections

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

History OOP languages Year Language 1967 Simula Smalltalk

A Quick Start Guide to DrJava

More on Objects and Classes

What's New in ADP Reporting?

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA - INNER CLASSES

Problem Solving. Software Engineering. Behavior. OCD in a Nutshell. Behavior. Objects Operations Algorithm. Object-Centered Design

The BlueJ Tutorial. Version for BlueJ Version 2.0.x. Michael Kölling Mærsk Institute University of Southern Denmark

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

Some Scanner Class Methods

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Advanced OOP Concepts in Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Transcription:

LAB4 Making Classes and Objects Objective The main objective of this lab is class creation, how its constructer creation, object creation and instantiation of objects. We will use the definition pane to do this lab. Definition Pane Definition Pane is the main window of DrJava, displaying the currently active source file. As you edit files in this window, DrJava helps out with several useful features. Such as "Find/Replace", "Go to Line", "Go to File", while also providing more advanced features like syntax coloring, automatic indentation, brace matching etc. Multiple Documents DrJava supports having multiple documents open at the same time. A list of all of the names of the open documents appears in a pane to the left of the Definitions Pane, listing files in the order in which they were opened. The Document List also has a context menu, which can be used by right-clicking on any document in the list. 1

Classes The class forms the basis for object oriented programming in java. The concept which needs to be implemented in a java program must be encapsulated within a class. /* A simple java program. Save this file with the name Myclass.java. */ public class Myclass // program begins with a call to the main() public static void main(string args[ ]) System.out.println( This is a simple java program. ); Class is saved in a file with.java extension and the file name should be the same as the class name.the keyword class is used to define a new class. Program begins with a call to main().the keyword public enables the access from outside the class, main() is declared public because it is called from outside the class when the program starts. The keyword static allows main() to be called without having to instantiate a particular instance of the class this is necessary because main() is called by Java virtual machine (JVM) before any objects are made. The keyword void tells the compiler that main() does not return any value. A class can define instance variables and methods. Following class defines two instance variables. public class Myclass // instance variables int width; int height; // method void mymethod() System.out.println( Hello World ); By creating class object values can be assigned to instance variables and methods can be called. Myclass myobj = new Myclass(); //creating object myobj.width = 100; // assign values to myobj s instance variable myobj.mymethod(); // call method 2

Comments Comments are used to make the code easier to understand it describes what your code is doing. The '//' starts a comment and tells the computer to ignore everything else till the end of the current line. You can use '/*' followed at some point by '*/' for a multi-line comment. // This is single line comment. /* This is multi-line comment. Comments make code easier to understand. '/**' followed at some point by '*/' creates a JavaDoc comment. */ Creating a Project and Adding Java files To create a project click Project menu and select New option as shown below:. Select the directory and folder to store the project. Enter name of the project eg: (OOP_YourName) and click Save. 3

option. To add a file click File Menu select New. To save a file click File Menu and select Save Enter the name (same as class name) with.java extension select folder and click Save. 4

Exercise 1. Define a public class HelloWorld.It should print * Welcome *. 2. Define a public class AddressBook.It has three String attributes name, address, phone. It must have methods showname (), showaddress (), showphone(). Show methods will show the attributes. There must be a constructer that sets your name, address, & phone number to the class attributes. Make the main() method call each method and print values of name, address and phone using these methods. Hint: // showname() method signature void showname() // Add code here // showaddress() method signature void showaddress() // Add code here // showphone() method signature void showphone() // Add code here 3. Define a class that must have objects of World and Turtle classes. This class must have public method of createturtle(); that must create turtle when this function is called. When object is created its constructor must initialize World class instance. Also this class must contain public function named drawrectangle() which draw a rectangle through turtle on world. // createturtle() method signature public void createturtle() // write your code here 5

// createturtle() method signature public void drawrectangle() // write your code here 4. Create the following figure using World and Turtle classes. You have to use three different turtles to draw each rectangle. Make a class Drawing. Draw three methods smallrect(), mediumrect() and largerect() in the class. Write the code for creating small, medium and large rectangles in the smallrect(), mediumrect() and largerect() methods respectively. Make a constructor that instantiate the World class. Turtle class should be instantiated in each method separately. Make the main() method, create an object of Drawing class and call all the three methods. 6

public void smallrect() //Write code here public void mediumrect() //Write code here public void largerect() // write code here 7