1. Writing Simple Classes:



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

Sample CSE8A midterm Multiple Choice (circle one)

Introduction to Java

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

Introduction to Java. CS 3: Computer Programming in Java

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

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

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

CS 106 Introduction to Computer Science I

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

Java Crash Course Part I

Example of a Java program

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

JAVA for Beginners. 2 nd Edition. Riccardo Flask

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

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

Object Oriented Software Design

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

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

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

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

Object Oriented Software Design

Chapter 2 Introduction to Java programming

AP Computer Science Java Subset

ECE 122. Engineering Problem Solving with Java

Programming Languages CIS 443

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

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

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

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

An Incomplete C++ Primer. University of Wyoming MA 5310

Introduction to Programming

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

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

Building Java Programs

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

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

Java Programming Fundamentals

CS170 Lab 11 Abstract Data Types & Objects

Java Program Coding Standards Programming for Information Technology

java Features Version April 19, 2013 by Thorsten Kracht

Homework/Program #5 Solutions

Crash Course in Java

CS 1302 Ch 19, Binary I/O

Conditional Statements Summer 2010 Margaret Reid-Miller

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Install Java Development Kit (JDK) 1.8

Introduction to Python

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

El Dorado Union High School District Educational Services

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

Sources: On the Web: Slides will be available on:

Building Java Programs

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

Java Basics: Data Types, Variables, and Loops

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

Computer Programming I

Arrays. Introduction. Chapter 7

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

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

Contents. 9-1 Copyright (c) N. Afshartous

Software Engineering Techniques

Introduction to Java Lecture Notes. Ryan Dougherty

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

Java Interview Questions and Answers

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

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

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

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

Comp 248 Introduction to Programming

CS 111 Classes I 1. Software Organization View to this point:

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

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

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

Introduction to Python

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

Statements and Control Flow

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

CISC 181 Project 3 Designing Classes for Bank Accounts

A Brief Introduction to MySQL

Outline Basic concepts of Python language

Java programming for C/C++ developers

Building Java Programs

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

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Topic 11 Scanner object, conditional execution

Arrays in Java. Working with Arrays

Iteration CHAPTER 6. Topic Summary

The C Programming Language course syllabus associate level

Anatomy of Credit card Numbers

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

Chapter 2 Basics of Scanning and Conventional Programming in Java

Moving from CS 61A Scheme to CS 61B Java

Programmierpraktikum

INPUT AND OUTPUT STREAMS

Inheritance, overloading and overriding

Transcription:

The University of Melbourne Department of Computer Science and Software Engineering 433-254 Software Design Semester 2, 2003 Answers for Lab 2 Week 3 1. Writing Simple Classes: a) In Java systems, there is only one class with the main function, which initiates the execution of the whole system. Therefore, in order to test individual classes (i.e. unit testing), developers are usually writing a simple class with a main function (also known as a driver class) that simply calls all different functions of a target class and prints the results. Your task is to download the file Keyboard.java from: www.cs.mu.oz.au/254/keyboard/keyboard.html and write a simple driver program that tests the following methods from Keyboard.java: readstring() readword() readchar() readboolean() readint() readlong() readfloat() readdouble() // A class to execute one or more Keyboard methods class Test System.out.print("Please enter a string: "); String str = Keyboard.readString(); System.out.println("String = " + str); System.out.print("Please enter a word: "); String word = Keyboard.readWord(); System.out.println("word = " + word); System.out.print("Please enter a character: "); char ch = Keyboard.readChar(); System.out.println("char = " + ch); System.out.print("Please enter a boolean (true/false): "); boolean bool = Keyboard.readBoolean(); System.out.println("boolean = " + bool);

System.out.print("Please enter an int number: "); int numint = Keyboard.readInt(); System.out.println("int = " + numint); System.out.print("Please enter a long number: "); long numlong = Keyboard.readLong(); System.out.println("long = " + numlong); System.out.print("Please enter a float number: "); float numfloat = Keyboard.readFloat(); System.out.println("float = " + numfloat); System.out.print("Please enter a double number: "); double numdouble = Keyboard.readDouble(); System.out.println("double = " + numdouble); // end class b) Write a Java class Student to meet the following specification. - The class should be able to support a 5 digit student ID, student name, marks for 3 subjects. You should have methods to set and get each of the attributes, and calculate the average for the student. Write a tester program to test your class. You should create 2 or 3 students and write code to test the class. Aim - Understand how to define a class and create objects of the class. class Student // data members private String _studentnumber; private String _studentname; private int _markformaths; private int _markforenglish; private int _markforscience; // Set Student Name public void setstudentname(string studentname) _studentname = studentname; // Set the student number public void setstudentnumber(string studentnumber) _studentnumber = studentnumber;

// getnumber() // return student number public String getnumber() return _studentnumber; // getname() // return student name public String getname() return _studentname; // entermarks() // enter all subject marks given as args public void entermarks(int maths, int english, int science) _markformaths = maths; _markforenglish = english; _markforscience = science; // getmathsmark() // return mark for maths public int getmathsmark() return _markformaths; // getenglishmark() // return mark for English public int getenglishmark() return _markforenglish; // getsciencemark() // return mark for Science public int getsciencemark() return _markforscience; // calculateaveragemark() // return the average of the three marks public double calculateaveragemark() return ((_markformaths + _markforenglish + _markforscience) / 3.0);

// class StudentTester // tests the Student class above public class StudentTester String stud_num, stud_name; Student s1 = new Student(); Student s2 = new Student(); s1.setstudentname("britney Spears"); s1.setstudentnumber("11111"); s1.setstudentname("eddie Vedder"); s1.setstudentnumber("22222"); s1.entermarks(20, 60, 2); s2.entermarks(80, 75, 76); outdetails(s1); outdetails(s2); // Write the details to standard out public static void outdetails(student s) System.out.println("--------------------------"); System.out.println("Student Number:\t" + s.getnumber()); System.out.println("Student Name:\t" + s.getname()); System.out.println("Maths:\t\t" + s.getmathsmark()); System.out.println("English:\t" + s.getenglishmark()); System.out.println("Science:\t" + s.getsciencemark()); System.out.println("Average:\t" + s.calculateaveragemark()); 2. Last Week s Extra Work: If you haven t done the last week s extra exercises, you must do them this week! Here they are again: (a) Write a java program that takes your first name and last name as command line arguments to the program and displays your name and last name on separate lines. Aim: Understand the use of command line arguments. // FirstLast.java class FirstLast System.out.println("First Name : " + args[0]); System.out.println("Last Name : " + args[1]);

(b) Write a program that calculates the total wages based on the number of hours worked. The wages are calculated at a rate of 8.25 per hour for up to 40 hours and at the rate of 1.5 the standard rate for any hours greater than 40. Number of hours is a command line argument to the program. Hint: Use Integer.parseInt(String s) converts a string to an integer (To convert the number of hours from command line to integer). We will understand this as we get through the rest of the lectures and labs but use it for now. Aim: Understand the use of if-else and constants. // Wages.java class Wages final double RATE = 8.25; final int STANDARD = 40; double pay = 0.0; int hours = 0; hours = Integer.parseInt(args[0]); if (hours > STANDARD) pay = STANDARD * RATE + (hours - STANDARD)*1.5*RATE; else pay = STANDARD * RATE; System.out.println("Wages = " + pay); (c) Write a program to take the student s grade as an input argument and print the comments as follows 100 (Perfect Score), 90-100 (Excellent), 80-90 (Good), 70-80 (Above Average), 60-70 (Average) 50-60 (Below Average), 0-50 (Not Passing). Hint: use switch statement. Aim: Understand the use of switch statement. // GradeReport.java class GradeReport int grade, category;

grade = Integer.parseInt(args[0]); category = grade/10; switch (category) case 10: System.out.println ("Perfect Score"); case 9: System.out.println ("Excellent"); case 8: System.out.println ("Good"); case 7: System.out.println ("Above Average"); case 6: System.out.println ("Average"); case 5: System.out.println ("Below Average"); default: System.out.println ("Not Passing"); (d) Write a program to print all odd numbers between 1 and 20. Note : Use while loop. Aim: Understand the use of while loop. //OddNumbers.java class OddNumbers final int MAX_NUMBER = 20; int i = 1; while (i <= MAX_NUMBER) if (i%2 > 0) System.out.println(i); i++;