# Java Programs Algorithms

1 / 37
0
0
1171 days ago, 489 views
PowerPoint PPT Presentation
Another Simple Program. /***************************************//File: Camus.java//Author: Mary Eberlein//A straightforward first Java program//***************************************public class Camus{/print a Camus cite open static void main(String[] args) { System.out.println(\"Camus said: \"); System.out.print(\"Some individuals talk in their rest. \\nLecturers talk\"); System

### Presentation Transcript

Slide 1

﻿Java Programs + Algorithms "Reconsider, code once." - - Anonymous "Weeks of programming can spare you hours of arranging." - - Anonymous Plan for Today More Simple Java Programs Comments in Java Algorithms Problem Decomposition Static Methods Based on slides by Reges and Stepp at buildingjavaprograms.com

Slide 2

Another Simple Program/***************************************/File: Camus.java/Author: Mary Eberlein/A straightforward first Java program/*************************************** open class Camus {/print a Camus cite open static void main(String[] args) { System.out.println("Camus stated: "); System.out.print("Some individuals talk in their rest. \nLecturers talk " ); System.out.println( " while other individuals rest. " ); }

Slide 3

Camus Program Output Camus stated: Some individuals talk in their rest. Speakers talk while other individuals rest. Work out: Re-compose the Camus program so that the quote is contained in twofold quotes: Camus stated: " Some individuals talk in their rest. Instructors talk while other individuals rest. "

Slide 4

Practice Questions What is the yield of the accompanying println proclamations? System.out.println("\ta\tb\tc"); System.out.println("\\\\"); System.out.println("'"); System.out.println("\"\"\""); System.out.println("C:\nin\the descending spiral"); Write a println articulation to create this yield:/\/\\/\\\

Slide 5

Answers Output of each println explanation: a b c \\ " """ C: in he descending winding println proclamation to deliver the line of yield: System.out.println("/\\/\\\\/\\\\\\");

Slide 6

More Practice Programs What println proclamations will produce this yield? This program prints a quote from the Gettysburg Address. "Four score and seven years back, our 'fore fathers' delivered on this mainland another nation." What println explanations will produce this yield? A "quoted" String is "substantially" better on the off chance that you take in the guidelines of "escape sequences." Also, "" speaks to a vacant String. Remember: utilize \" rather than " ! "" is not the same as "

Slide 7

Answers println articulations to create the yield: System.out.println("This program prints a"); System.out.println("quote from the Gettysburg Address."); System.out.println(); System.out.println("\"Four score and seven years ago,"); System.out.println("our 'fore fathers' delivered on"); System.out.println("this mainland another nation.\""); println proclamations to produce the yield: System.out.println("A \"quoted\" String is"); System.out.println("'much' better in the event that you learn"); System.out.println("the principles of \"escape sequences.\""); System.out.println(); System.out.println("Also, \"\" speaks to a discharge String."); System.out.println("Don't overlook: utilize \\\" rather than \" !"); System.out.println("'' is not the same as \"");

Slide 8

Comments remark : A note written in source code by the developer to portray the code and make it less demanding to get it. Remarks are not executed when your program runs. Punctuation:/remark content, on one line or,/* remark content; may traverse various lines */Examples:/This is a one-line remark. /* This is a long multi-line remark. */

Slide 9

Using remarks Where to place remarks: at the highest point of each record (a "comment header") - distinguishes the creator and depicts what the program does toward the begin of each strategy (seen later) - portrays the technique's conduct to clarify complex bits of code inside strategies Comments are helpful for: Understanding bigger, more mind boggling programs. Various developers cooperating, who must see each other's code.

Slide 10

Comments case/* Suzy Student, CS 305j, Fall 2099 This program prints the Powerpuff Girls signature melody. */open class PowerpuffSong { open static void main(String[] args) {/first verse System.out.println( " Blossom, she is administrator and leader,"); System.out.println( " Bubbles, she is a delight and a laughter,"); System.out.println( " Buttercup, she " s the hardest warrior " ); System.out.println( " Powerpuffs spare the day. " ); System.out.println();/second verse System.out.println( " Fighting crime,trying to spare the world"); System.out.println( " Here they come in the nick of time, the Powerpuff Girls"); }

Slide 11

Algorithms calculation : A rundown of ventures for taking care of an issue. Illustration calculation: "Bake sugar cookies" Mix the dry fixings. Cream the margarine and sugar. Beat in the eggs. Mix in the dry fixings. Set the stove temperature. Set the clock. Put the treats into the broiler. Permit the treats to heat. Spread icing and sprinkles onto the treats. ...

Slide 12

Problems with calculations absence of structure : Many little strides; intense to recall. repetition : Consider making a twofold bunch... Blend the dry fixings. Cream the margarine and sugar. Beat in the eggs. Mix in the dry fixings. Set the broiler temperature. Set the clock. Put the primary cluster of treats into the broiler. Permit the treats to heat. Set the clock. Put the second clump of treats into the stove. Permit the treats to heat. Blend elements for icing. ...

Slide 13

Structured calculations organized calculation : a rundown of ventures to take care of an issue, separated into intelligent assignments. 1 Make the treat player. Blend the dry fixings. Cream the spread and sugar. Beat in the eggs. Mix in the dry fixings. 2 Bake the treats. Set the stove temperature. Set the clock. Put the treats into the broiler. Permit the treats to heat. 3 Add icing and sprinkles. Blend the elements for the icing. Spread icing and sprinkles onto the treats. ...

Slide 14

Removing repetition An all around organized calculation can portray rehashed errands with less excess. 1 Make the treat hitter. Blend the dry fixings. ... 2a Bake the treats (first group). Set the stove temperature. Set the clock. ... 2b Bake the treats (second bunch). 3 Decorate the treats. ...

Slide 15

A program with excess open class BakeCookies { open static void main(String[] args) { System.out.println("Mix the dry ingredients."); System.out.println("Cream the margarine and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients."); System.out.println("Set the stove temperature."); System.out.println("Set the timer."); System.out.println("Place a bunch of treats into the oven."); System.out.println("Allow the treats to bake."); System.out.println("Set the broiler temperature."); System.out.println("Set the timer."); System.out.println("Place a clump of treats into the oven."); System.out.println("Allow the treats to bake."); System.out.println("Mix elements for frosting."); System.out.println("Spread icing and sprinkles."); }

Slide 16

class strategy An announcement proclamation explanation technique B articulation strategy C proclamation explanation articulation Static strategies static strategy : A named gathering of articulations. means the structure of a program wipes out excess by code reuse procedural deterioration : isolating an issue into techniques Writing a static strategy resembles adding another order to Java.

Slide 17

Using static techniques 1. Plan the calculation. Take a gander at the structure, and which charges are rehashed. Choose what are the essential general undertakings. 2. Proclaim (record) the techniques. Organize explanations into gatherings and give each gathering a name. 3. Call (run) the techniques. The program's primary strategy executes alternate techniques to play out the general assignment.

Slide 18

Design of a calculation/This program shows a tasty formula for preparing treats. open class BakeCookies2 { open static void main(String[] args) {/Step 1: Make the cake hitter. System.out.println("Mix the dry ingredients."); System.out.println("Cream the spread and sugar."); System.out.println("Beat in the eggs."); System.out.println("Stir in the dry ingredients.");/Step 2a: Bake treats (first bunch). System.out.println("Set the stove temperature."); System.out.println("Set the timer."); System.out.println("Place a clump of treats into the oven."); System.out.println("Allow the treats to bake.");/Step 2b: Bake treats (second bunch). System.out.println("Set the broiler temperature."); System.out.println("Set the timer."); System.out.println("Place a group of treats into the oven."); System.out.println("Allow the treats to bake.");/Step 3: Decorate the treats. System.out.println("Mix elements for frosting."); System.out.println("Spread icing and sprinkles."); }

Slide 19

Static Methods static technique: an arrangement of proclamations which is given a name, so it can be executed in a program. Static strategies streamline our projects by wiping out repetition and breaking our program into reasonable, firm pieces. Separating an issue into static strategies is called procedural deterioration . Utilizing a static technique requires: Write the strategy - record an arrangement of explanations and give them a name. This resembles composing