1 / 75

Arrays

Arrays. Exam 1. 1. Java source and executable files have the following extensions? a. .java and .class b. .src and .class c. .javadoc and .exe d. .list and .exe. Exam 1. 2. Which of the following is a Java comment a. ; This is my first program b. # This is my first program

ulla
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays

  2. Exam 1 1. Java source and executable files have the following extensions? a. .java and .class b. .src and .class c. .javadoc and .exe d. .list and .exe

  3. Exam 1 2. Which of the following is a Java comment a. ; This is my first program b. # This is my first program c. /* This is my first program */ d. ‘ This is my first program e. none of the above

  4. Exam 1 3. We use the following operator to create a new object in Java a. malloc b. new c. string d. newObject

  5. Exam 1 4. Commands to compile and execute a java program are a. run and java b. execute and javac c. javac and java d. compile and run

  6. Exam 1 5. Identifiers may start with a. $ b. @ c. a number d. &

  7. Exam 1 6. It is necessary to declare an object before you can use it a. True b. False

  8. Exam 1 7. Which of the following is a valid identifier? a. Bank Account b. bank account c. bank$account d. bank-account

  9. Exam 1 8. A series of characters that appear in double quote is a char data type a. True b. False

  10. Exam 1 9. boolean isDone == false; is a valid assignment statement in Java a. True b. False

  11. Exam 1 10. Which of the following is a correct variable declaration statement? a. int x - float y; b. int x: float y; c. int x,y; d. Long int x;

  12. Exam 1 11. Boolean expressions are built by use of _relational______operators and _boolean_______operators 12. A data value that can not be changed is called __constant____________ 13. $Bad-Variable is __bad/invalid/not _ Java identifier.

  13. Exam 1 14. These two data types: __float__, and ___double___are used for real numbers 15. _An object____is an instance of a class

  14. Exam 1 16. double loanPeriod; if (loanPeriod < 0 || >1000 ); { System.out.println(“Loan period is invalid”); System.exit(1); } • loanPeriod is not initialized • loanPeriod or a variable name is missing • ; is not needed.

  15. Exam 1 17. double s; s = 1.0; switch (s) { case 1.0: System.out.println(“ March madness”); break; case 2.0: System.out.println(“ November rain”); break; case 3.0: System.out.println(“White Christmas”); break; default: System.out.println(“No Special name”); break; } 1. Switch doesn’t support double data type

  16. Exam 1 18. char aChar = ”NFL Championship”; • Char datatype can only contain 1 character • Char data type needs single quotes instead of double ones

  17. Exam 1 19. int i, j; double x,y; i=1; j=2; x= Math.pow(3, (i/j)); y = x % 2; if (y ==0) System.out.println(x +" is an even number "); else System.out.println(x +" is an odd number"); ½ = 0, 30=1.0 y = 1.0 % 2 = 1.0 1.0 is an odd number

  18. Exam 1 20. int count=1, sum=0; while (count < 9) { if (count % 2 != 0) { sum += count; } count++; } System.out.println(" Sum ="+ sum);

  19. Exam 1 count =1, sum =0, 1 %2 = 1 (!= 0), sum = 0+1=1 count =2, sum=1, 2 % 2 = 0 count =3, sum=1, 3 % 2 = 1 (!=0), sum = 1+3 = 4 count =4, sum=4, 4 % 2 = 0 count =5, sum=4, 5 % 2 = 1 (!=0), sum = 4+5 = 9 count =6, sum=9, 6 % 2 = 0 count =7, sum=7, 7 % 2 = 1 (!=0), sum = 9+7 = 16 count =8, sum=16, 8 % 2 = 0 count =9, exit Sum = 16

  20. Exam 1 21. int sum =0; for (int i=1; i<=2; i++) { for (int j=1; j<=3; j++) { sum = sum + (i+j); } } System.out.println(" Sum ="+ sum);

  21. Exam 1 sum =0 i=1; j=1; sum=0+(1+1) j=2; sum =(1+1)+(1+2) j=3; sum = (1+1)+(1+2)+(1+3) = 2+3+4=9 i=2 j=1; sum=9+(2+1) j=2; sum=9+(2+1)+(2+2) j=3; sum=9+(2+1)+(2+2)+(2+3)= 9+(3+4+5)=9+12=21 Sum =21

  22. Lab 4

  23. Project Discussion (section 01+02) • Loan class: public class Loan{ // data members // constructor(s) // Methods() }

  24. Project Discussion (section 01) • Loan class: public class Loan{ // data members private int loanPeriod; private double loanAmount; private double monthlyInterestRate; private double monthlyAmortization; // optional }

  25. Project Discussion (section 01) • Loan class: public class Loan{ //Constructor(s) public Loan(double loanAmt, double interest, int months ) { // this is a stub loanAmount = loanAmt; // should initialize monthlyInterestRate and //loanPeriod the same way } }

  26. Project Discussion (section 01) • Loan class: public class Loan{ // Methods public void setLoanAmount(double loanAmt) { // this is stub } public double getLoanAmount() { return loanAmount; } }

  27. Project Discussion (section 01) • Loan class: public class Loan{ // Methods public void setMonthlyInterestRate(double interest) { // this is stub } public double getMonthlyInterestRate() { return monthlyInterestRate; } }

  28. Project Discussion (section 01) • Loan class: public class Loan{ // Methods public void set(int months) { // this is stub } public double getLoanPeriod() { return loanPeriod; } }

  29. Project Discussion (section 01) • Loan class: public class Loan{ // Methods public double computeAmortization() { // this is a stub // The formula should be put here // Make sure to change the names to the data // member name // Don’t forget return ……; } }

  30. Project 2 (Section 01) public class Amortization{ // data member; private Loan loan; // constructor public Amortization () { } }

  31. Project 2 (Section 01) public class Amortization{ // Methods: public void start() { // This is a stub } public void describeProgram() { // This is a stub } }

  32. Project 2 (Section 01) public class Amortization{ // Methods: public void getInput () { // This is a stub // You should use Loan class here } public void displayOutput() { // This is a stub // You should use Loan class here } }

  33. Lesson plan • Arrays

  34. Section 02 public class Loan { // data members private double monthlyInterestRate; private double loanAmount; private int numberOfMonths; }

  35. Section 02 public class Loan { // Constructor(s) public Loan(double borrowedAmt, double interest, int months) { // this is a stub loanAmount= borrowedAmt; // similarly, you can initialize monthlyInterestRate // and numberOfMonths accordingly } }

  36. Project 2 (Section 02) public class Amortization{ // data member private Loan loan; }

  37. Project 2 (Section 02) public class Amortization{ // constructor public Amortization() { // this is a stub loan = new Loan(1000,0.1,12); } }

  38. Project 2 (Section 02) public class Amortization{ // methods public void start() { // this is a stub } private/public void describeProgram() { // this is a stub // Cut and paste System.out.print describe what // this program is doing from project 1 to here } }

  39. Project 2 (Section 02) Import javax.swing.*; public class Amortization{ // methods public/private void getInput() { // this is a stub // Cut and paste the while loop to get inputs users in part e (project 1) here // Use Loan Class loan.setBorrowedMoney( …..); } }

  40. Project 2 (Section 02) Import javax.swing.*; public class Amortization{ // methods public/private void printOutput() { // This is a stub // print out loan. // getAmortizationPayment() } }

  41. Section 02 public class Loan { //Methods public void setBorrowedMoney(double principle) { // this is a stub } public double getBorrowedMoney() { return loanAmount; } }

  42. Section 02 public class Loan { //Methods public void setMonthlyInterestRate(double interest) { // this is a stub } public double getMonthlyInterestRate() { return monthlyInterestRate; } }

  43. Section 02 public class Loan { //Methods public void setNumberOfMonths(int months) { // this is a stub } public double getNumberOfMonths() { return months; } }

  44. Section 02 public class Loan { //Methods public double getAmortizationPayment() { // this is a stub // You should put the formula in project 1 here // Don’t forget to change the variable names // and don’t forget to return the value; return ……; } }

  45. Arrays • We often need to group together related items of data. • Cards in a pack. • Ships in a port. • Java provides two distinct facilities: • Traditional array. • Flexible-size collection classes (java.util).

  46. Problems That Arrays Solve …… minValue = firstNumber; if (secondNumber < minValue) minValue = secondNumber; if (thirdNumber < minValue) minValue = thirdNumber; if (fourthNumber < minValue) minValue = fourthNumber; if (fifthNumber < minValue) minValue = fifNumber; …… What is this code doing? Finding minimum value from a set of 5 values

  47. Arrays for Numerical data type (primitive data types) int[] number = new int [5]; int minValue; String inputStr; for (int i=0; i<5; i++) { inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i); number[i] = Integer.parseInt(inputStr); } minValue = number[0]; for (int i=1; i<5; i++) { if (minValue > number[i]) minValue = number[i]; } System.out.println(" minValue ="+ minValue); Array declaration & allocation memory

  48. Arrays for Numerical data type (primitive data types) Getting values for all elements in the array int[] number = new int [5]; int minValue; String inputStr; for (int i=0; i<5; i++) { inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i); number[i] = Integer.parseInt(inputStr); } minValue = number[0]; for (int i=1; i<5; i++) { if (minValue > number[i]) minValue = number[i]; } System.out.println(" minValue ="+ minValue);

  49. Arrays for Numerical data type (primitive data types) int[] number = new int [5]; int minValue; String inputStr; for (int i=0; i<5; i++) { inputStr = JOptionPane.showInputDialog(null,"Please enter the value for element "+i); number[i] = Integer.parseInt(inputStr); } minValue = number[0]; for (int i=1; i<5; i++) { if (minValue > number[i]) minValue = number[i]; } System.out.println(" minValue ="+ minValue); Access each element of an array

More Related