...OBJECT ORIENTED PROGRAMMING IN JAVA ‐ EXERCISES CHAPTER 1 1. Write Text‐Based Application using Object‐Oriented Approach to display your name. // filename: Name.java // Class containing display() method, notice the class doesnt have a main() method public class Name { public void display() { System.out.println("Mohamed Faisal"); } } // filename: DisplayName.java // place in same folder as the Name.java file // Class containing the main() method public class DisplayName { public static void main(String[] args) { Name myname = new Name(); // creating a new object of Name class myname.display(); // executing the display() method in the Name class } } 2. Write a java Applet to display your age. // filename: DisplayNameApplet.java import java.applet.Applet; // import necessary libraries for an applet import java.awt.Graphics; public class DisplayNameApplet extends Applet { public void paint(Graphics g) { g.drawString("Mohamed Faisal", 50, 25); } } // filename: DisplayNameApplet.htm // place in same folder as the compiled DisplayNameApplet.class file Displaying my Name CHAPTER 2 3. Write a program that calculates and prints the product of three integers. // filename: Q1.java import java.util.Scanner; // import Scanner libraries for input public class Q1 { public static void main(String[] args) { Scanner input = new Scanner (System.in); int number1; int number2; int number3; System.out.println("Enter the First Number"); www.oumstudents...
Words: 3130 - Pages: 13
...End of Chapter Review and Programming Exercises 10212010 LIST OF REVIEW AND PROGRAMMING EXERCISES PER CHAPTER CHAPTER 1: ASSIGNED REVIEW AND PROGRAMMING EXERCISES Please complete these end of chapter review and programming exercises. They were selected because they are relevant to the competencies you must develop to complete the performance assessment. Complete R1.6, R1.7, R1.11, R1.13, R1.14, R1.18. Complete P1.1, P1.8, P1.10 CHAPTER 2: ASSIGNED REVIEW AND PROGRAMMING EXERCISES Please complete these end of chapter review and programming exercises. They were selected because they are relevant to the competencies you must develop to complete the performance assessment. Complete R2.1, R2.3, R2.4, R2.9, R.2.14, R.2.15 Complete P2.1, P2.2, P2.4, P2.5, P2.8, P2.9, P2.12, P2.13, P2.14, P2.19 CHAPTER 3: ASSIGNED REVIEW AND PROGRAMMING EXERCISES Please complete the following end of chapter review and programming exercises. They were selected because they are relevant to the competencies you must develop to complete the performance assessment. Complete R3.2, R3.3, R3.4, R3.7, R3.8, R3.9, R3.10, R3.11, R3.13, R3.18, R.3.21 Complete P3.1, P3.2, P3.3, P3.4, P3.5, P3.6, P3.9, P3.10, P3.12, P3.14, P.3.17, P3.23, P3.25 End of Chapter Review and Programming Exercises 10212010 CHAPTER 4: ASSIGNED REVIEW AND PROGRAMMING EXERCISES Please complete the following end of chapter review and programming exercises. They were selected because they are relevant to the competencies...
Words: 1463 - Pages: 6
...that has made his work popular with programmers for many years. Michael Schidlowsky and Sedgewick have developed concise new Java implementations that both express the methods in a natural and direct manner and also can be used in real applications. Algorithms in Java, Third Edition, Part 5: Graph Algorithms is the second book in Sedgewick's thoroughly revised and rewritten series. The first book, Parts 1-4, addresses fundamental algorithms, data structures, sorting, and searching. A forthcoming third book will focus on strings, geometry, and a range of advanced algorithms. Each book's expanded coverage features new algorithms and implementations, enhanced descriptions and diagrams, and a wealth of new exercises for polishing skills. The natural match between Java classes and abstract data type (ADT) implementations makes the code more broadly useful and relevant for the modern object-oriented programming environment. The Web site for this book (www.cs.princeton.edu/~rs/) provides additional source code for programmers along with a variety of academic support materials for educators. Coverage includes: A complete overview of graph properties and types Diagraphs and DAGs Minimum spanning trees Shortest paths Network flows Diagrams, sample Java code, and detailed algorithm descriptions A landmark revision, Algorithms in Java, Third Edition, Part 5 provides a complete tool set for programmers to implement, debug, and use graph algorithms...
Words: 281 - Pages: 2
...Pasig campus pangilinan DSITI-AI Oct 27 15 Java Programming java programming Introduction in java programming Objectives * Object-Oriented Programming Language * Object-Oriented Programming Principle * Benefits Of Object-Oriented Programming * Introduction To Java Programming * Resources Used To Create a Java Programming *Structures Of a Java Programming * Result Of Executing The Java Programing Object Oriented Programming Language (OOPL) OOPL Is An Extension Of Procedural Language. Involves Creating Program Components as Object Related To The Real Word. Writing Object-Oriented Programs Involves Creating Object And Application That Uses Those Objects. An Object Contains Both Data Procedures Can be packaged Into a Single Unit. Based On Three Concepts Encapsulation Ability To Bind Data And Procedures Into an Object. Inheritance Ability Of Objects To Acquire The Attributes Or Behavior Of Other Objects Or Classes. Polymorphism Ability of An Object To Take Many Forms Or Identities. Benefits Of Object-Oriented Programming *Reusability -Able To Reuse The Defined Objects. *Adaptability –Able to fit in different environment. *maintainability –Able to change easily. *reliability –Able to operate...
Words: 1445 - Pages: 6
...Question Compile the two test files (BankAccount.java first and then Program2.java second). Execute Program2 with the following inputs: starting balance - $500 (don't enter the dollar sign) monthly interest rate - 0.00125 (this is a 1.5% annual rate) monthly pay - $1000 (don't enter the dollar sign) withdrawal amount - $900 (don't enter the dollar sign) Verify that you earn $0.75 in interest and have an ending balance at the end of the month of $600.75. Then modify the BankAccount class's constructor method to create a BankAccount object which stores a monthly interest when the user inputs an annual interest rate of the format "nnn.nn" (i.e. 1.5). Note that the BankAccount constructor stored a monthly interest rate for the BankAccount object's instance field originally, but the user had to convert the annual rate to a monthly rate (i.e. 1.5 to 0.00125). Then modify the Program2 driver class to prompt the user for an annual interest rate. Recompile both classes and execute the modified Program2 driver class again, this time with following inputs: starting balance - $500 (don't enter the dollar sign) annual interest rate - 1.5 monthly pay - $1000 (don't enter the dollar sign) withdrawal amount - $900 (don't enter the dollar sign) Verify that you still earn $0.75 in interest and still have an ending balance at the end of the month of $600.75 as you did with the original code. Submit only the modified source code...
Words: 1152 - Pages: 5
...between abstract and concrete classes. To declare abstract methods to create abstract classes. How polymorphism makes systems extensible and maintainable. To determine an object’s type at execution time. To declare and implement interfaces. Why art thou cast down, O my soul? —Psalms 42:5 ■ ■ © Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Chapter 10 Object-Oriented Programming: Polymorphism 307 Assignment Checklist Name: Section: Date: Exercises Assigned: Circle assignments Date Due Prelab Activities Matching Fill in the Blank Short Answer Programming Output Correct the Code YES YES YES YES YES YES 1 NO NO 1 YES NO NO NO NO NO NO Lab Exercises Exercise 1 — Payroll System Modification Follow-Up Question and Activity Follow-Up Question and Activity Debugging Exercise 2 — Accounts Payable System Modification YES Labs Provided by Instructor 1. 2. 3. Postlab Activities Coding Exercises Programming Challenges 1, 2, 3, 4, 5, 6, 7, 8 1, 2 © Copyright 1992-2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Chapter 10 Object-Oriented Programming: Polymorphism 309 Prelab Activities Matching Name: Section: After reading Chapter 10 of...
Words: 4624 - Pages: 19
... Bina Nusantara University Jakarta Tugas Personal ke-1 Minggu 2 Review Questions: 1. Java source filename extension is “.java”, and the Java bytecode filename extension is ".class" 2. Java is case sensitive? Java distinguish between uppercase and lowercase. The case for Java keywords, such as "String", “string”, "Int", “int”, etc, are different. 3. There is no effect on runtime performance between: a. Import java.util.Scanner; b. Import java.util.*; The facts are: • There is no runtime cost from using an import statement • The compilation process can take a little more time with an import statement • The compilation process can take even more time with a wildcard import statement • For improved readability, wildcard import statements are bad practice for anything but throwaway classes • The compilation overhead of non-wildcard import statements are minor, but they give readability benefits so best practice is to use them 4. Pseudo-code and flowchart to create: a. an algorithm to calculate a triangle area [pic] b. an algorithm to change second into hour and minute unit [pic] 5. Describe and give example for each method of String a. contains The java string contains() method searches the sequence of characters in this string. It returns true if sequence of...
Words: 1109 - Pages: 5
...A – Z), or an underscore (_). * The name can only consist of letters, numbers, and underscores (no special characters). * Variable names are case sensitive. * The variable name cannot be identical as a reserved name in Python due to the fact that Python uses them for other things. Variable Naming Rules of Java * Variable names are case sensitive. * A variable name can be any length as long as it is within the Unicode letter and digit standard, also you should use a letter as the first character of the variable word. You are allowed to use the dollar sign or underscore ($, _ ) as a first character but it isn’t recommended. * The only characters other than letters and number that can be used is the dollar sign and underscore (“$”, “_”), and no other special characters may be allowed. * If you are only using one word for the variable name it is advised to use all lower case. In the case of the variable name being more than one word you should capitalize the first letter of the following word or words. * You cannot use a reserved word or keyword for the variable name. Similarities and Differences of Visual Basic, Python, and Java Similarities | Differences | 1. You are...
Words: 702 - Pages: 3
...Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. This tutorial introduces how to declare array variables, create arrays, and process arrays using indexed variables. Declaring Array Variables: To use an array in a program, you must declare a variable to reference the array, and you must specify the type of array the variable can reference. Here is the syntax for declaring an array variable: ------------------------------------------------- dataType[] arrayRefVar; // preferred way. ------------------------------------------------- ------------------------------------------------- or ------------------------------------------------- ------------------------------------------------- dataType arrayRefVar[]; // works but not preferred way. Note: The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers. Example: The following code snippets are...
Words: 1665 - Pages: 7
...Java Quick Reference Console Input Scanner input = new Scanner(System.in); int intValue = input.nextInt(); long longValue = input.nextLong(); double doubleValue = input.nextDouble(); float floatValue = input.nextFloat(); String string = input.next(); Console Output System.out.println(anyValue); JOptionPane.showMessageDialog(null, "Enter input"); GUI Input Dialog String string = JOptionPane.showInputDialog( "Enter input"); int intValue = Integer.parseInt(string); double doubleValue = Double.parseDouble(string); Message Dialog Primitive Data Types byte short int long float double char boolean 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits 16 bits true/false Arithmetic Operators + * / % ++var --var var++ var-addition subtraction multiplication division remainder preincrement predecrement postincrement postdecrement Assignment Operators = += -= *= /= %= assignment addition assignment subtraction assignment multiplication assignment division assignment remainder assignment Relational Operators < >= == != less than less than or equal to greater than greater than or equal to equal to not equal Logical Operators && || ! ^ short circuit AND short circuit OR NOT exclusive OR if Statements if (condition) { statements; } if (condition) { statements; } else { statements; } if (condition1) { statements; } else if (condition2) { statements; } else { statements; } switch Statements switch (intExpression) { case value1: statements; break; ... case valuen: statements; break;...
Words: 73366 - Pages: 294
...functions, methods and classes within the Java programming environment. This laboratory involves the creation of a number of Java programs. Make sure that you save any code you write. Also make sure you save any results or notes that you observe about your work. Note that you are unlikely to complete this worksheet in just one laboratory session. 2 Preliminaries Create a project in Eclipse called CS1002_Lab5 and create a corresponding class (say CS1002_Lab5). Try and organise your work (from the following exercises) into separate methods as we did in the previous worksheet. 3 Strings Copy the following code into your project and run the program. public static void main(String args[]) { double number = 1.0/3.0; System.out.println(number); DecimalFormat number_format = new DecimalFormat("#.##"); String formatted_string = number_format.format(number); System.out.println(formatted_string); } Is it showing you an error? Can you fix it? The DecimalFormat class enables us to format numbers (and other classes) in a variety of ways. In the above example we are formatting the number to two decimal places. Read up on this class in the JavaDocs as we will need it later. http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html To complete the following exercise you will also need a good understanding of the String class: http://docs.oracle.com/javase/6/docs/api/java/lang/String.html http://docs.oracle.com/javase/tutorial/java/data/strings.html...
Words: 2314 - Pages: 10
...CertPrs8/OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)/Sierra/177200-6/FM CONTENTS AT A GLANCE Part I OCA and OCP 1 Declarations and Access Control .............................. 3 2 Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 3 Assignments 4 Operators 5 Working with Strings, Arrays, and ArrayLists .................... 257 6 Flow Control and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 .............................................. 165 ................................................ 223 Part II OCP 7 Assertions and Java 7 Exceptions ............................. 377 8 String Processing, Data Formatting, Resource Bundles . . . . . . . . . . . . . . 417 9 I/O and NIO 477 10 Advanced OO and Design Patterns 11 Generics and Collections .............................................. ............................ 541 .................................... 573 12 Inner Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 13 Threads 713 14 Concurrency 15 JDBC A About the CD Index .................................................. .............................................. 785 ................................................... 841 .....
Words: 2596 - Pages: 11
...A Programmer’s Guide to Java™ SCJP Certification Third Edition This page intentionally left blank A Programmer’s Guide to Java™ SCJP Certification A Comprehensive Primer Third Edition Khalid A. Mughal Rolf W. Rasmussen Upper Saddle River, New Jersey • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sidney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United...
Words: 15086 - Pages: 61
...An interface is a reference type, similar to a class, which can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. In Java, a class can inherit from only one class but it can implement more than one interface. | Superclass Instance Method | Superclass Static Method | Subclass Instance Method | Overrides | Generates a compile-time error | Subclass Static Method | Generates a compile-time error | Hides | An abstract class is a class that is declared abstract. Abstract classes cannot be instantiated, but they can be subclassed. It can contain abstract methods that did not have implemented. Subclasses then provide the implementations for the abstract methods. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this: Except for the Object class, a class has exactly one direct superclass. A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. (Note that hiding fields is generally bad programming practice.) The Object class is the top of the class hierarchy. All classes are descendants from this class and inherit methods from it. Useful methods inherited from Object include toString(), equals(), clone(), and getClass()...
Words: 328 - Pages: 2
...LAB EXERCISE 1: READ FROM URL URLReader.java import java.net.*; import java.io.*; public class URLReader { public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.lelong.com/"); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; for ( int i =0;(inputLine = in.readLine()) != null; i++){ System.out.println(inputLine);} in.close(); } } LAB EXERCISE 2: Echo Client EchoClient .java import java.io.*; import java.net.*; public class EchoClient { public static void main(String[] args) { try { Socket s = new Socket("127.0.0.1", 9999); BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter w = new PrintWriter(s.getOutputStream(), true); BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); String line; do { line = r.readLine(); if ( line != null ) System.out.println(line); line = con.readLine(); w.println(line); } while ( !line.trim().equals("bye") ); } catch (Exception err) { System.err.println(err); } } } LAB EXERCISE 3:MULTI THREADS SimpleThreads.java public class SimpleThreads { //Display a message, preceded by the name of the current thread static void threadMessage(String message) { String threadName = Thread.currentThread().getName(); System...
Words: 1219 - Pages: 5