Answers to Assignment #1 (Assignment due: Wed. Feb. 05, 2003) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
Quick Sorting -Ed. 2. and 3.: Chapter 10 -Ed. 4.: Chapter 11.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Java Programming (Chapter 1). Java Programming Classes, Types, and Objects.
Using break and continue
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
LAB 10.
Computer Programming Lab(5).
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 14: Recursion J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Object-Oriented Programming Simple Stack Implementation.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Output Programs These slides will present a variety of small programs. Each program has some type of array that was introduced in this chapter. Our concern.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
1 Input/Output. 2 In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Java linked list.
Method overloading contd class OverloadDemo { public static void main(String args[]) { Overload ob = new Overload(); int resI; double resD; // call all.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Slides by Evan Gallagher
Slides by Evan Gallagher
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Exercise Java programming
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
OUTPUT STATEMENTS GC 201.
Computing Adjusted Quiz Total Score
The for-loop and Nested loops
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
(Assignment due: Wed. Feb. 04, 2004)
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Methods and Data Passing
Building Java Programs
Building Java Programs
Presentation transcript:

Answers to Assignment #1 (Assignment due: Wed. Feb. 05, 2003) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize the basic concepts used in Java, such as class, method, variable, Constructor,... (as many as possible) 3. Write a program that reads in an integer number and prints the following output as long as the number is odd and greater than 0 and less than 10. For example, if the number entered is 9, print:

import java.lang.*; public class as1q3 { public static void main(String args[]){ int j=5; int a=Integer.parseInt(args[0]); int num=1; int line=1; int i; if(a%2==1 && a>0 && a<10) { while(num<=a) { for(i=1; i<=(j-line); i++) { System.out.print(" "); } for(i=(j-line+1); i<=(j+line-1); i++) { System.out.print(num); } System.out.println(); ++line; num+=2; }

4. Write a recursive method called computeFN which computes the following function for values of N: f(N) = 3*f(N-3) + 4*f(N-2) - 5*f(N-1) where f(0) = 1, f(1) = 1, f(2) = 3.

import java.lang.*; public class as1q4 { public static int computeFN(int N) { int result_0 = 1; int result_1 = 1; int result_2 = 3; int result = 0; if(N<0){ System.out.println("N can not be negative !");} switch(N) { case 0: result = result_0; break; case 1: result = result_1; break; case 2: result = result_2; break; } if(N>2){ result = 3*computeFN(N-3) + 4*computeFN(N-2) - 5*computeFN(N-1); } return result; } public static void main(String arg[]) { int result; int N=Integer.parseInt(arg[0]); result=computeFN(N); System.out.println("N = "+N+" F("+N+") = "+result); }

5. Implement ”quick sort" and sort a sequence containing 20 integers: 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15. Trace 10 steps of the computation. import java.lang.*; public class as1q5 { public static int count_steps = 1; public static void sort(int[] a, int from, int to) { if((a==null)||(a.length<2)||(to-from<=0)) return; int i=from, j=to; int center=a[(from+to)/2]; i--; j++; do { i++; j--; while((i<to)&&(a[i]<center)) i++; while((j>from)&&(a[j]>center)) j--; if(i<j) { int tmp=a[i]; a[i]=a[j]; a[j]=tmp; //print the result of each step }

} while(i<=j); if(from<j){ sort(a,from,j); } if(i<to){ sort(a,i,to); } public static void main(String args[]) { // Create an array to hold numbers int nums[]={3,4,6,1,10,9,5,20,19,18,17,2,1,14,13,12,11,8,16,15}; //print the input array for(int k=0; k<nums.length; k++) System.out.print(nums[k]+", "); System.out.println(" *INPUT*"); sort(nums,0,(nums.length-1)); }

Trace: 3, 4, 6, 1, 10, 9, 5, 20, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 15 3, 4, 6, 1, 10, 9, 5, 15, 19, 18, 17, 2, 1, 14, 13, 12, 11, 8, 16, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 18, 17, 2, 1, 14, 13, 12, 11, 8, 19, 20 3, 4, 6, 1, 10, 9, 5, 15, 16, 8, 17, 2, 1, 14, 13, 12, 11, 18, 19, 20 j i

3, 4, 6, 1, 10, 9, 5, 15, 16, 8, 17, 2, 1, 14, 13, 12, 11 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 17, 2, 1, 14, 13, 12, 16 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 12, 2, 1, 14, 13, 17, 16 j i 3, 4, 6, 1, 10, 9, 5, 15, 11, 8, 12, 2, 1, 14, 13 3, 4, 6, 1, 10, 9, 5, 13, 11, 8, 12, 2, 1, 14, 15 j i

3, 4, 6, 1, 10, 9, 5, 13, 11, 8, 12, 2, 1, 14 3, 4, 1, 1, 10, 9, 5, 13, 11, 8, 12, 2, 6, 14 3, 4, 1, 1, 2, 9, 5, 13, 11, 8, 12, 10, 6, 14 3, 4, 1, 1, 2, 5, 9, 13, 11, 8, 12, 10, 6, 14 j i 3, 4, 1, 1, 2, 5 1, 4, 1, 3, 2, 5

1, 1, 4, 3, 2, 5 j i 1, 1 j i 1 1