Java for Non Majors Spring 2018

Similar documents
Java for Non Majors. Final Study Guide. April 26, You will have an opportunity to earn 20 extra credit points.

Introduction to Programming Using Java (98-388)

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

15CS45 : OBJECT ORIENTED CONCEPTS

Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

CS 251 Intermediate Programming Methods and More

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

This page intentionally left blank

CS 251 Intermediate Programming Methods and Classes

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

5/23/2015. Core Java Syllabus. VikRam ShaRma

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Mathematics/Science Department Kirkwood Community College. Course Syllabus. Computer Science CSC142 1/10

PESIT Bangalore South Campus

Selected Questions from by Nageshwara Rao

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

CH. 2 OBJECT-ORIENTED PROGRAMMING


C# Programming for Developers Course Labs Contents

CMSC 331 Second Midterm Exam

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

Computer Science II (20073) Week 1: Review and Inheritance

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Arrays Classes & Methods, Inheritance

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS171:Introduction to Computer Science II

Java Object Oriented Design. CSC207 Fall 2014

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Chapter 14 Abstract Classes and Interfaces

COP 3330 Final Exam Review

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

C++ Important Questions with Answers

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS 251 Intermediate Programming Inheritance

Polymorphism. return a.doublevalue() + b.doublevalue();


Rules and syntax for inheritance. The boring stuff

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Full file at Chapter 2 - Inheritance and Exception Handling

OVERRIDING. 7/11/2015 Budditha Hettige 82

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Murach s Beginning Java with Eclipse

Inheritance and Polymorphism

Computer Science II (20082) Week 1: Review and Inheritance

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Inheritance and Polymorphism

Class, Variable, Constructor, Object, Method Questions

VIRTUAL FUNCTIONS Chapter 10

Brief Summary of Java

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

C ONTENTS PART I FUNDAMENTALS OF PROGRAMMING 1. and Java 3. Chapter 1 Introduction to Computers, Programs,

Midterm assessment - MAKEUP Fall 2010

CO Java SE 8: Fundamentals

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

WA1278 Introduction to Java Using Eclipse

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Compaq Interview Questions And Answers

Objects as a programming concept

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

IQTIDAR ALI Lecturer IBMS Agriculture University Peshawar

INHERITANCE. Spring 2019

1 Shyam sir JAVA Notes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

Object Oriented Programming with Java. Unit-1

Inheritance and Polymorphism

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Chapter 4 Defining Classes I

Course Outline. Introduction to java

More on Objects in JAVA TM

Building Java Programs

(2½ hours) Total Marks: 75

Java Programming Training for Experienced Programmers (5 Days)

The Islamic University Gaza Department of Electrical & Computer Engineering. Midterm Exam Spring 2012 Computer Programming II (Java) ECOM 2324

Programming II (CS300)

Practice for Chapter 11

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CS 11 java track: lecture 3

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

More Relationships Between Classes

First IS-A Relationship: Inheritance

Big software. code reuse: The practice of writing program code once and using it in many contexts.

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java 1.8 Programming

Programming Exercise 14: Inheritance and Polymorphism

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Transcription:

Java for Non Majors Spring 2018 Final Study Guide The test consists of 1. Multiple choice questions - 15 x 2 = 30 points 2. Given code, find the output - 3 x 5 = 15 points 3. Short answer questions - 3 x 5 = 15 points 4. Code writing questions - 10 x 2 + 20 = 40 points Please try and attempt all questions. You get points for trying. The test is cumulative, but focuses on topics introduced after the midterm. Partial points would be given even if there are syntax error for code writing questions. Topics to study Basics of Java Programming. You will be expected to be familiar with Java Syntax, the basic components of a Java program, compiling and running code and the tools involved, etc. Questions will draw from (but not be limited to) the following topics: Java reserved words. Data types, variables and type conversions. Operators and operator precedence. Console I/O. Basic input output Printing using the System class methods - println, print and printf. Reading in data from te console using the Scanner class. Selection and Repetition. Regular sequential execution of code. Logical operators and short circuit evaluations. Selection statement - if, if-else, if-else ladders, switch structure (including case labels, use of break and continue) Loops - while, do-while and for loops, enhanced for loops, choosing loops most suited for a particular task. Java Methods. Method syntax - declaring and defining methods. 1

Modifiers, return types and parameter lists. Strings Arrays Pass by value and pass by reference. Method overloading. Difference between the String class and the StringBuilder class. String comparison. Using methods of the String class and the StringBuilder class. Declaring, initializing and using arrays of primitive types. Passing arrays as parameters to methods and returning arrays from methods. Multi-dimensional arrays. Classes and Objects Creating an object of a pre-existing class and using the available methods. Access Modifiers - public, private and protected. Defining a class - data attributes, constructors, accessor and mutator methods, instance methods. Instantiating a class (creating an object), and using instance methods. Difference between static and instance methods. Passing objects as parameters to methods and returning objects from methods. The this keyword. Arrays of objects. Inheritance, Interfaces and Polymorphism Concept of base (super) and derived (sub) classes. The super() keyword. Method overriding. The class Object Abstract Classes The concept of delayed or dynamic binding. Casting classes Interfaces Java Exceptions Types of exceptions. Claiming, throwing and catching exceptions. The finally keyword. GUI programming and Graphics Basic Graphics Classes in Java (Swing and AWT). Events and event driven programming The paint() methods, and the Graphics, Color and Polygon classes. 2

Sample Questions 1. General Instructions The multiple choice questions and short answer questions will pretty much be like the questions on the midterm. Some samples for the code writing questions are included in the study guide. Please note that the sample questions are of the same difficulty level as the questions on the test. That is the only similarity between the questions here and those on the test. You will be expected to solve a different problem on the test. The Sample Runs provided here and on the test are samples. They are just a single run of the program. Your solution should work for all legal inputs. Please do not hard code your solution (think that the sample is the only possible input) to the sample run. The sample is just provided to clarify the requirement. For example, if the sample run says N=4, it does not mean N is 4 all the time. It just happens to be 4 this once. It can be any integer. 2. Code Writing: Loops Write a Java method called armstrong to print all the Armstrong Numbers in a given range. This method accepts 2 integer parameters - start and end and returns nothing. An Armstrong Number is a 3 digit number where the sum of the cubes of the individual digits of the number equals the number itself. For example, 371 is an Armstrong Number, since 3 3 + 7 3 + 1 3 = 27 + 343 + 1 = 371. Sample Run: (start = 100, end = 999) The Armstrong Numbers between 100 and 999 are: 153 370 371 407 Solution: Please note that only the method is required. You need not write the entire program. public void armstrong ( int start, int end) System.out.println("The Armstrong Numbers between " + start + " and " + end + " are:"); //Run a loop from start to end. Check each number for(int i=start; i<=end; i++) int num = i; int sum = 0; //Summing the cubes of individual digits. while(num!=0) int digit = num % 10; // get the digit in the units place sum = sum + (int) Math.pow(digit,3); // cube and add to sum num = num / 10; // get rid of the current units place 3

//Check if the sum equals the original number if (sum == i) System.out.println(i); 3. Code Writing: String Manipulation Write a Java method called insert3 that reads a series of strings from the user and prints them after inserting the string blue at the third index of the given string. The method accepts a single integer parameter N that denotes the number of strings. The function returns nothing. The strings entered by the user will always be at least 4 characters long. You need not test for that. Sample Run: (N = 4) Enter the 4 strings: Hello World Helbluelo World To infinity and beyond To blueinfinity and beyond I Wumbo You Wumbo I Wblueumbo You Wumbo Java Programming Javbluea Programming Solution: Once again, we only need to write the method, not the entire class. You can assume that all the necessary libraries have been imported. You can make this assumption of the test as well. public void insert3 (int N) Scanner in = new Scanner (System.in); System.out.println("Enter the " + N + " strings:"); //run a loop to read and process the N strings. for (int i =0; i < N; i++) //StringBuilder has an available "insert" method. String doesn t. //Create the buffer directly using the user input. StringBuilder sb1 = new StringBuilder ( in.nextline()); sb1.insert(3, "blue"); System.out.println(sb1); 4. Code Writing: Inheritance + Array of Objects Write a Java program with the following requirements. Write a class called Drink. The class has an integer attribute called size that holds the size of the drink in ounces. The class also has a parametrized constructor and a method called print that prints the size. 4

Write a class called Coffee that inherits from Drink. The class has an integer attribute called numshots that denotes number of espresso shots in the drink. The class also has a parametrized constructor and a method called print that overrides the base class method to print both the size and the number of shots. Write a class called Beer that inherits from Drink. The class has a double attribute called percentalcohol that denotes percentage of alcohol content in the drink. The class also has a parametrized constructor and a method called print that overrides the base class method to print both the size and the percentage of alcohol. Write a class called ManyDrinks. This call just contains the main method. In the main method, create an array of Drink of size 4. The first 2 elements are of type Coffee, and the last 2 are Beer. Give the drinks values of your choice. Then invoke the print method for each of them. Sample Run: Drink 1 Size : 16 oz Number of espresso shots : 2 Drink 2 Size : 20 oz Number of espresso shots : 4 Drink 3 Size : 12 oz Aclohol content : 4.2% Drink 4 Size : 25 oz Aclohol content : 8.6% Solution: Here, you can assume that the classes have their accessor and mutator methods. You need not write them. This code is fairly self explanatory. //This is the base class / superclass class Drink int size; public Drink(int s) size = s; public void print() System.out.println("Size : " + size + " oz"); // First Subclass class Coffee extends Drink int numshots; public Coffee (int s, int n) 5

super(s); numshots = n; public void print() super.print(); System.out.println("Number of espresso shots : " + numshots); //Second Subclass class Beer extends Drink double percentalcohol; public Beer ( int s, double p) super(s); percentalcohol = p; public void print() super.print(); System.out.println("Aclohol content : " + percentalcohol + "%"); public class ManyDrinks public static void main(string [] args) //Create an array of the Drink class of size 4 Drink [] drinkarray = new Drink[4]; // Populate the array as per requirements. drinkarray[0] = new Coffee (16, 2); drinkarray[1] = new Coffee (20,4); drinkarray[2] = new Beer (12, 4.2); drinkarray[3] = new Beer (25, 8.6); //Call the print method after printing the drink number. for (int i=0; i< 4; i++) System.out.println("Drink " + (i+1)); drinkarray[i].print(); 6