Computer & Programming I Quiz #5 Dr. H. Assadipour 1. What is the output of the following program?

Similar documents
Interactive Programs and Graphics in Java

Chapter 2 Introduction to Java programming

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

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

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

AP Computer Science Static Methods, Strings, User Input

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

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

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

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

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

Homework/Program #5 Solutions

Introduction to Java. CS 3: Computer Programming in Java

JAVA ARRAY EXAMPLE PDF

Introduction to Java

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

Introduction to Programming

Object-Oriented Programming in Java

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

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

Basics of Java Programming Input and the Scanner class

Sample CSE8A midterm Multiple Choice (circle one)

Building Java Programs

CS506 Web Design and Development Solved Online Quiz No. 01

Graphical User Interfaces

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

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

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

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

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

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

CS170 Lab 11 Abstract Data Types & Objects

Topic 11 Scanner object, conditional execution

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Building Java Programs

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Comp 248 Introduction to Programming

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

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

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

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:

Java CPD (I) Frans Coenen Department of Computer Science

More on Objects and Classes

CS 335 Lecture 06 Java Programming GUI and Swing

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

Week 1: Review of Java Programming Basics

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Chapter 2: Elements of Java

Essentials of the Java(TM) Programming Language, Part 1

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

Using Files as Input/Output in Java 5.0 Applications

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

Construction of classes with classes

Essentials of the Java Programming Language

CSE 8B Midterm Fall 2015

CS 106 Introduction to Computer Science I

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

How to Convert an Application into an Applet.

Chapter 1 Java Program Design and Development

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

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

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

Using A Frame for Output

Lösningsförslag till tentamen

Install Java Development Kit (JDK) 1.8

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

AP Computer Science Java Subset

Animazione in Java. Animazione in Java. Problemi che possono nascere, e loro soluzione. Marco Ronchetti Lezione 1

java Features Version April 19, 2013 by Thorsten Kracht

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

Introduction to Object-Oriented Programming

Advanced Network Programming Lab using Java. Angelos Stavrou

Introduction to Java Applets (Deitel chapter 3)

Building Java Programs

CS 121 Intro to Programming:Java - Lecture 11 Announcements

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

Building Java Programs

Arrays in Java. Working with Arrays

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

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

public class demo1swing extends JFrame implements ActionListener{

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

Chapter 2 Elementary Programming

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

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

Lab 5: Bank Account. Defining objects & classes

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

Importance of the Java Inet Address and Public String Hosting System

Java Crash Course Part I

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

Swing. A Quick Tutorial on Programming Swing Applications

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Visit us at

Java 進 階 程 式 設 計 03/14~03/21

For live Java EE training, please see training courses

13 File Output and Input

Cours de Java. Sciences-U Lyon. Java - Introduction Java - Fondamentaux Java Avancé.

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

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

Transcription:

Computer & Programming I Quiz #5 Dr. H. Assadipour 1. What is the output of the following program? Driver Program public class ThingTester public static void main (String[] args) Thing one = new Thing(); System.out.println (one); one.putthing(5); System.out.println (one); Thing two = new Thing(); two.putthing(9); System.out.println (two); Thing three = new Thing(); three.putthing(11); System.out.println (one); System.out.println (two); System.out.println (three); Class Thing public class Thing private int x; private int y; private static int z = 0; public Thing() x = z + 1; y = z + 5; static void putthing( int a) z = a; public String tostring () return (" x = " + x + " y = " + y + " z = " + z + "\n"); ----jgrasp exec: java ThingTester x = 1 y = 5 z = 0 x = 1 y = 5 z = 5 x = 6 y = 10 z = 9 x = 1 y = 5 z = 11 x = 6 y = 10 z = 11 x = 10 y = 14 z = 11

2. Write a method called average that accepts two integer parameters and returns their average as a floating point value. public double average (int num1, int num2) return (num1 + num2) / 2.0; 3. Overloadd the average method of Exercise 6.1 such that if three integers are provided as parameters, the method returns the average of all three. public double average (int num1, int num2, int num3) return (num1 + num2 + num3) / 3.0; 4. Write a method called multiconcat that takes a String and an integer as parameters. Return a String that consists of the string parameter concatenated with itself count times, where count is the integer parameter. For example, if the parameter values are "hi" and 4, the return value is "hihihihi". Return the original string if the integer parameter is less than 2. public String multiconcat (String text, int count) String result = text; if (repeats > 1) for (int i=2; i <= count; i++) result += text; return result; Implementation 1 import java.util.scanner; public class Concat_Driver public static void main(string[] args) Scanner s=new Scanner(System.in); String text = s.nextline(); int repeats = s.nextint(); String result = text; if (repeats > 1) for (int i=2; i <= repeats; i++) result += text; System.out.println("The multi-concatenated string is: " + result );

Implementation 2: Driver import java.util.scanner; public class Concat_Driver_01 public static void main(string[] args) String results; canner s=new Scanner(System.in); String text = s.nextline(); int repeats = s.nextint(); Multi_Concat obj = new Multi_Concat (text, repeats); results = obj.multiconcat(text, repeats); System.out.println("The multi-concatenated string is: " + results ); Implementation 2: Driver: Class public class Multi_Concat private String text; private int repeats; public Multi_Concat (String s, int i) text = s; repeats = i; public String multiconcat (String text, int repeats) String result = text; if (repeats > 1) for (int i=2; i <= repeats; i++) result += text; return result; ----jgrasp exec: java Concat_Driver_01 hi 5 The multi-concatenated string is: hihihihihi ----jgrasp: operation complete.

5. Write a method called drawcircle that draws a circle based on the method's parameters: a Graphics object through which to draw the circle, two integer values representing the (x, y) coordinates of the center of the circle, another integer that represents the circle's radius, and a Color object that defines the circle's color. The method does not return anything. public void drawcircle (Graphics page, int x, int y, int rad, Color color) page.setcolor (color); page.drawoval (x rad, y rad, rad*2, rad*2); 6. Overload the drawcircle method of Exercise 6.6 such that if the Color parameter is not provided, the circle's color will default to black. public void drawcircle (Graphics page, int x, int y, int rad) page.setcolor (Color.black); page.drawoval (x rad, y rad, rad*2, rad*2);

7. Rewrite the following Applet program as an Application. //******************************************************************** // ConcentricCircles.java Preparer: Your Name // // Solution to Programming Project 5.23 (5E, p. 288) //******************************************************************** import javax.swing.japplet; import java.awt.*; public class ConcentricCircles extends JApplet private final int NUM_CIRCLES = 10; private final int MIN_RADIUS = 5; private final int MAX_RADIUS = 140; private final int CENTER_X = 150; private final int CENTER_Y = 150; //----------------------------------------------------------------- // Paints several concentric circles of random radius. //----------------------------------------------------------------- public void paint(graphics page) int radius, x, y; setbackground (Color.black); page.setcolor (Color.green); for (int count=1; count <= NUM_CIRCLES; count++) radius = (int) (Math.random() * MAX_RADIUS) + MIN_RADIUS; x = CENTER_X - radius; y = CENTER_Y - radius; page.drawoval (x, y, radius*2, radius*2);

8. What is the output of the following program? import java.awt.*; import javax.swing.*; public class NameOrientation public static void main (String[] args) JFrame frame = new JFrame ("Name Orientation"); frame.setdefaultcloseoperation (JFrame.EXIT_ON_CLOSE); JPanel primary = new JPanel(); primary.setbackground (Color.cyan); primary.setpreferredsize (new Dimension(200, 50)); JLabel firstname = new JLabel ("John"); JLabel lastname = new JLabel ("Smith"); primary.add (firstname); primary.add (lastname); frame.getcontentpane().add(primary); frame.pack(); frame.setvisible(true);