Byung-Hyun Ha bhha@pusan.ac.kr 프로그래밍 실습 #1 Byung-Hyun Ha bhha@pusan.ac.kr.

Slides:



Advertisements
Similar presentations
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
TA: Nouf Al-Harbi NoufNaief.net :::
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Computer Programming Lab(4).
Announcements Quiz 2 Grades Posted on blackboard.
Computer Programming Lab(5).
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java TA Session 1. Software Java Runtime Environment (JRE) java.exe Java Development Kit (JDK) java.exe, javac.exe Version 1.6 = Version 6, Version 1.7.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
A: A: double “4” A: “34” 4.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
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.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
Programación I Estructuras de control Java. IF- Else.
Chapter 2 Clarifications
CSC111 Quick Revision.
Software Development I/O and Numbers
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Exercise Java programming
Compiling and Running a Java Program
Chapter 6 Queue.
Multiple if-else boolean Data
Computer Programming Methodology Input and While Loop
Repetition.
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
הרצאה 3 אלמנטים בסיסיים בשפה
The for-loop and Nested loops
חלק ה שימוש במציין שלם לערך תווי
מיונים וחיפושים קרן כליף.
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
ניתוח זמן ריצה (על קצה המזלג)
Java so far Week 7.
AP Java Warm-up Boolean Array.
Chapter 6 Queue.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 6 Queue.
Lecture Notes – Week 4 Chapter 5 (Loops).
Module 3 Selection Structures 4/5/2019 CSE 1321 Module 3.
Building Java Programs
Repetition Statements
Consider the following code:
Presentation transcript:

Byung-Hyun Ha bhha@pusan.ac.kr 프로그래밍 실습 #1 Byung-Hyun Ha bhha@pusan.ac.kr

정수를 사용한 연산: 섭씨를 화씨로 import java.util.Scanner; public class Temp { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Temp. in degree Celsius? "); int c = s.nextInt(); int f = c * 9 / 5 + 32; System.out.println("It's " + f + " deg. F."); }

실수를 사용한 연산: 섭씨를 화씨로 import java.util.Scanner; public class Temp { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Temp. in degree Celsius? "); double c = s.nextDouble(); double f = c * 9 / 5 + 32; System.out.println("It's " + f + " deg. F."); }

실수에 관하여 import java.util.Scanner; public class Big { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); for (int i = 0; i < 10000; i++) { n = n + 0.0001; } System.out.println("Result: " + n);

실수에 관하여 1을 입력해보자 10000000000을 입력해보자 (동그라미가 10개) 어떤 결과 vs. 실제 결과 10000000000을 입력해보자 (동그라미가 10개) 1000000000000000을 입력해보자 (동그라미가 15개)

문자열 import java.util.Scanner; public class Name { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Name? "); String name = s.nextLine(); int n = name.indexOf(' '); String sir = name.substring(0, n); String given = name.substring(n + 1, name.length()); System.out.print("Sir name is " + sir + " and given name is " + given); }

Java 2 Platform API Specification

반올림: 잘못된 프로그램 import java.util.Scanner; public class Round { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); double r = (int)(n + 0.5); System.out.println(n + " is rounded off to " + r); }

반올림 import java.util.Scanner; public class Round { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); double r = Math.floor(n + 0.5); System.out.println(n + " is rounded off to " + r); }

약수1 import java.util.Scanner; public class Divisor { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); for (int i = 1; i <= n; i++) { if ((n % i) == 0) { System.out.print(i + " "); }

약수2 import java.util.Scanner; public class Divisor { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); for (int i = 1; i <= n; i++) { if ((n / i * i) == n) { System.out.print(i + " "); }

소수 (Prime Number) import java.util.Scanner; public class Prime { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); boolean isPrime = true; for (int i = 2; i < n; i++) { if (n / i * i == n) { isPrime = false; break; } if (isPrime) { System.out.println("Prime Number"); } else { System.out.println("Not Prime Number");

숫자 피라미드 출력 Input number (less than 10)? 6 010 01210 0123210 012343210 010 01210 0123210 012343210 01234543210 0123456543210

숫자 피라미드 출력 import java.util.Scanner; public class Pramid { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number (less than 10)? "); int n = s.nextInt(); for (int i = 0; i <= n; i++) { for (int j = 0; j < n - i; j++) { System.out.print(' '); } for (int j = 0; j <= i; j++) { System.out.print(j); for (int j = i - 1; j >= 0; j--) { System.out.println();

숫자 n개 입력 (999를 입력하면 끝) import java.util.Scanner; public class InputN { public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] ns = new int[1000]; int count = 0; for (int i = 0; i < 1000; i++) { System.out.print((i + 1) + "'th: "); ns[i] = s.nextInt(); if (ns[i] == 999) { count = i; break; } System.out.print("Your input: "); for (int i = 0; i < count; i++) { System.out.print(ns[i] + " ");

정렬 public class Sort { public static void main(String[] args) { int[] ns = {4, 5, 1, 7, 1, 3}; for (int i = 0; i < ns.length - 1; i++) { for (int j = i + 1; j < ns.length; j++) { if (ns[i] > ns[j]) { int temp = ns[i]; ns[i] = ns[j]; ns[j] = temp; } System.out.println("Result: "); for (int i = 0; i < ns.length; i++) { System.out.print(ns[i] + " ");

빈도수 구하기 public class Frequency { public static void main(String[] args) { int[] ns = {4, 5, 1, 7, 1, 3, 1, 3, 1, 4}; int[] frequency = new int[20]; for (int i = 0; i < 20; i++) { frequency[i] = 0; } for (int i = 0; i < ns.length; i++) { frequency[ns[i]]++; System.out.println(i + ": " + frequency[i]);