First, your program should include this array of Strings: String[] strings = {"Jo","Jim","Dana","Wilma","Lana","Ned, Nathaniel","Jill","Joe"};

Similar documents
Introduction to Java

Sample CSE8A midterm Multiple Choice (circle one)

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

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

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

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

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

JAVA ARRAY EXAMPLE PDF

Basics of Java Programming Input and the Scanner class

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

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

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Introduction to Java. CS 3: Computer Programming in Java

Topic 11 Scanner object, conditional execution

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:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

AP Computer Science Static Methods, Strings, User Input

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

CSE 8B Midterm Fall 2015

Introduction to Programming

Object-Oriented Programming in Java

Chapter 2 Introduction to Java programming

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

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

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

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

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Comp 248 Introduction to Programming

Visit us at

(Eng. Hayam Reda Seireg) Sheet Java

Arrays in Java. Working with Arrays

Install Java Development Kit (JDK) 1.8

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

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

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

Programming Fundamentals I CS 110, Central Washington University. November 2015

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

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

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

Building Java Programs

Building Java Programs

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

AP Computer Science Java Subset

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

The following program is aiming to extract from a simple text file an analysis of the content such as:

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

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Building Java Programs

LOOPS CHAPTER CHAPTER GOALS

More on Objects and Classes

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Using Files as Input/Output in Java 5.0 Applications

In this Chapter you ll learn:

Inheritance, overloading and overriding

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

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

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

Homework/Program #5 Solutions

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Programming Languages CIS 443

Building Java Programs

java Features Version April 19, 2013 by Thorsten Kracht

Java Interview Questions and Answers

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

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

Chapter 2 Basics of Scanning and Conventional Programming in Java

Explain the relationship between a class and an object. Which is general and which is specific?

COMPUTER SCIENCE. Paper 1 (THEORY)

Java CPD (I) Frans Coenen Department of Computer Science

For live Java EE training, please see training courses

Interactive Applications (CLI) and Math

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

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

D06 PROGRAMMING with JAVA

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

D06 PROGRAMMING with JAVA

Software Development with UML and Java 2 SDJ I2, Spring 2010

Unit 6. Loop statements

The Snake Game Java Case Study

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Lab 5: Bank Account. Defining objects & classes

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

public static void main(string[] args) { System.out.println("hello, world"); } }

JAVA.UTIL.SCANNER CLASS

CmpSci 187: Programming with Data Structures Spring 2015

Threads 1. When writing games you need to do more than one thing at once.

Arrays. Introduction. Chapter 7

Transcription:

CS 121 Spring 2014 - Midterm II 1. (30) Write a complete, stand alone program in a single class called StringWork, which works as follows: First, your program should include this array of Strings: String[] strings = "Jo","Jim","Dana","Wilma","Lana","Ned, Nathaniel","Jill","Joe"; Your program should read in a non- negative int value from the keyboard - say n, and then print, in a column, all strings that are shorter (smaller length) than n. For example if n is 3 then only Jo should be printed. Note: your program should include the strings array, as shown, but to save time you can simply enter this line in your program: String[] strings =..as above..; import java.util.scanner; public class StringWork Scanner scan = new Scanner(System.in); String[] strings = "Jo","Jim","Dana","Wilma","Lana","Ned, Nathaniel","Jill","Joe"; int n = scan.nextint(); for(int i = 0; i < strings.length; i++) if(strings[i].length() < num) System.out.println(strings[i]); //end of for loop //could also use a for each loop for(string s: strings) if((s.length() < num) System.out.println(s); //end of for each loop //end of class 2. The method mystery, below, from the RecurFns class is recursive. public static void mystery(int[] s, int k) if ((k >= 0) && (k < s.length)) System.out.print(s[k]); mystery(s,k- 1);

Suppose a is this array: 2,4,6 What happens when you execute each of the following statements: RecurFns.mystery(a,- 1); Nothing RecurFns.mystery(a,0); 2 RecurFns.mystery(a,1); 42 RecurFns.mystery(a,2); 642 RecurFns.mystery(a,3); Nothing 3. Suppose arr is any array of ints, and assume that arr has at least one element. 3a. Write a single statement that prints to the screen the very last element in arr. System.out.println(arr[arr.length - 1]); 3b Write a code fragment (in general requiring multiple lines) that exchanges the value stored at the very last position in arr and the value at the first position (value at index position 0). int temp = arr[0]; arr[0] = arr[arr.length - 1]; arr[arr.length - 1] = temp; 3c Write a code fragment that prints the average value in the array arr (in general this won t be a whole number). double average = 0.0; for(int i = 0; i < arr.length; i++) average+=arr[i]; System.out.println(average/arr.length); 3d Write a code fragment that prints the largest value in the array arr (in case of ties, any will do).

//set max to the lowest number possible int max = Integer.MIN_VALUE; //can also set it to the first value in the array max = arr[0]; for(int i = 0; i < arr.length; i++) if(arr[i] > max) max = arr[i]; System.out.println(max); 3e Use a for- each loop to print in a column all even values in the array arr. for(int num: arr) if(num % 2 == 0) System.out.println(num) 4 Consider the following code fragment. Rewrite it using a while loop. Here s is any String. int count = 0; s = s.tolowercase(); for(int j =0; j < s.length(); j++) if (Character.isLetter(s.charAt(j))) count++; System.out.println(count); int count = 0; s = s.tolowercase(); int j = 0; //initialize j outside of loop while(j < s.length()) if (Character.isLetter(s.charAt(j))) count++; j++; //increment j outside of if statement System.out.println(count);

5. A phone company offers cell service, and uses this simple Phone class to model its accounts. It offers service according to three different plans, 1, 2, and 3. public class Phone private String customer; private int plan; public Phone(String customer, int plan) this.customer = customer; this.plan = plan; public String getcustomer()return owner; public int getplan()return plan; public it setplan(int p)plan = p; // sets level of service public int tostring()return(customer + + plan); 5a A Phone plan object with a plan value of 1 is considered to be a low service plan. When the plan value is 2 or 3, the plan is considered a high service plan. Write a method, to be added to the Phone class, called lowservice, which takes no arguments, and which returns true if its calling object has plan value 1; otherwise the method should return false. public boolean lowservice() if(this.plan == 1) return true; else return false; 5b The phone company above has decided to offer landline service as well as cell service. To reflect this, write the MultiPlan class, a subclass of the Phone class with one new attribute, landline, of type boolean. When landline is true, the customer s phone service includes both cell service (at some plan level) and landline service. When landline is false, the customer is getting only cell service. Your MultiPlan class must involve one extra attribute, landline. The MultiPlan constructor must use super. Also, there must be getlandline and setlandline methods. Finally, MultiPlan should have its own tostring method, which returns a String that gives a customer s name, plan level, and landline status (true or false).

public MultiPlan extends Phone private boolean landline; public MultiPlan(String customer, int plan) super(customer, plan); landline = false; public void setlandline(boolean newlandline) landline = newlandline; public boolean getlandline() return landline; 5c In a single statement in a driver class make a MultiPlan object called danaacct, with customer Dana, plan level 3, and no landline. public class MultiPlanDriver public static void main(string[] args) MultiPlan danaacct = new MultiPlan( Dana, 3, false); 5d Write static method fancyservice in the MultiPlan class, which takes an array of MultiPlan objects as a parameter, and which prints to the screen the names of all customers with plans that include both the highest service (plan attribute value equals 3), and a landline. public static void fancyservice(multiplan[] plans) for(multiplan p : plans) if(p.getplan() == 3 && p.landline) //need to print out just the name System.out.println(p.getCustomer());