Preview

Object Oriented Programming Note

Good Essays
Open Document
Open Document
1229 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Object Oriented Programming Note
1. Rewrite the following counter-control program to do while loop and for loop, in order to print out the biggest number inputted via keyboard. Scanner keyboard = new Scanner(System.in); int max = -1; int n; int c = 0; while (c<10) { n = keyboard.nextInt(); if (n > max) { max = n; } c++; } System.out.print(max);
Do while loop: Scanner keyboard = new Scanner(System.in); int max, n, c; max = -1; c =0; do { n = keyboard.nextInt(); if (n > max) { max = n; } c++;} while (c<10); System.out.print(max);
For loop: Scanner keyboard = new Scanner(System.in); int max, n, c; max = -1; c = 0; for (c=0; c<10; c++) { n = keyboard.nextInt(); if (n > max) { max = n; } } System.out.print(max);

2. Rewrite the following event control program to while loop and for loop. Scanner keyboard = new Scanner(System.in); int total = 0; int c = 1; int n = keyboard.nextInt(); while(c<2*n) { System.out.print(" "+c); total = total + c; c = c* 2; } System.out.println(total);
While loop: Scanner keyboard = new Scanner(System.in); int total, c, n; total = 0; c=1; n = keyboard.nextInt(); do { if (n >0) { System.out.print(" "+c); total = total + c; c = c* 2; } } while(c<2*n); System.out.print(total);
For loop: Scanner keyboard = new Scanner(System.in); int total, c, n; total = 0; n = keyboard.nextInt(); for (c=1; c<2*n; c = c* 2) { if (n >0) { System.out.print(" "+c); total = total + c; } } System.out.print(total);
3. Provide the value printed on the screenshot: public class While1 { public static void main(String [] args) { int x = 1; while(x < 10) { System.out.println("x = " + x); x++; }

You May Also Find These Documents Helpful

  • Satisfactory Essays

    PT1420 Unit 8 Lab 8

    • 365 Words
    • 2 Pages

    Step 7: Write a for loop that will display the numbers starting at 20, then 40, then 60,…

    • 365 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Nt1110 Unit 3 Assignment

    • 479 Words
    • 2 Pages

    Modern electronic calculators contain a keyboard with buttons for digits and arithmetical operations. Some even contain 00 and 000 buttons to make large numbers easier to enter. Most basic calculators assign only one digit or operation on each button. However, in more specific calculators, a button can perform multi-function working with key combination or current reckoning…

    • 479 Words
    • 2 Pages
    Powerful Essays
  • Satisfactory Essays

    A computer keyboard is an input device used to enter characters and functions into the computer system by pressing buttons or keys.it is the primary device used to enter text. A keyboard is used to enter text . a keyboard typically contains keys for individual letters, numbers and…

    • 1042 Words
    • 5 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Nt1310 Unit 3 Assignment

    • 256 Words
    • 2 Pages

    Write a program to read the name of 5 students through keyboard and store them in a file “name.txt”.…

    • 256 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Pt1420 Assignment

    • 1058 Words
    • 5 Pages

    2) Design a Do-While loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user if he or she wishes to perform the operation again. If so, the loop should repeat, otherwise it should terminate…

    • 1058 Words
    • 5 Pages
    Satisfactory Essays
  • Good Essays

    unit 8 homework

    • 793 Words
    • 5 Pages

    4. Design a loop that asks the user to enter a number. The loop should iterate 10 times and keep a running total of the numbers entered.…

    • 793 Words
    • 5 Pages
    Good Essays
  • Satisfactory Essays

    12. The STO→ button stores numbers to variables. To evaluate the expression23 4 a c b + −, press 9 STO→ ALPHA MATH ENTER to store the number 9 to A. Repeat this same process if B = 2 and C = 1, then evaluate the expression by…

    • 641 Words
    • 3 Pages
    Satisfactory Essays
  • Satisfactory Essays

    9.) A bug collector collects bugs every day for 7 days. Design a program that keeps a running total of the number of bugs collected during the 7 days. The loop should ask for the number of bugs collected each day, and when the loop is finished, the program should display the total number of bugs collected.…

    • 651 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Please select the number of one of these options: 1. Sum the numbers in the list 2. Multiply the numbers in the list Your choice: 1 Answer is: 16 Exiting program... */ public class Project1 { //public class ReadFile { //…

    • 779 Words
    • 4 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Assignment 2

    • 338 Words
    • 2 Pages

    3.) Input, and display, the total of the numeric input after each input is entered. Average the numeric input, indicate lowest numeric input value and the highest numeric input value for the previous numeric inputs, before the next numeric input is asked for. (Example given in class)…

    • 338 Words
    • 2 Pages
    Satisfactory Essays
  • Satisfactory Essays

    IT Level 3

    • 1133 Words
    • 6 Pages

    The program tells us how to multiply the numbers that are put above, e.g. found coins = 20, magic coins =10 and stolen coins=3 which are from lines 1-3 and line4 shows us a question which consists of those numbers. Line 5 shows us that stolen coins value is now 2 instead of 3 and line7 shows us that magic coins has also changed its value from 10 to 13…

    • 1133 Words
    • 6 Pages
    Satisfactory Essays
  • Satisfactory Essays

    Design a modular program that asks the user to enter a distance in kilometers, and the converts that distance to miles. The conversion formula is as follows:…

    • 620 Words
    • 5 Pages
    Satisfactory Essays
  • Satisfactory Essays

    In this tutorial I will show you how to code two of the number buttons (since all 10 are the same except the zero button), how to code the calculations buttons, the clear buttons and the backspace buttons. Before writing any code you need to add the following variables to the top (Globals):…

    • 2742 Words
    • 11 Pages
    Satisfactory Essays
  • Good Essays

    import java.util. *; class LRU { static int nof,nop,i,index,m,frames[]=new int[100],fs[]=new int [100],n,miss=0,hit=0,l=0,flag1,flag2; public static void main(String args[]) { Scanner s=new Scanner(System.in); System.out.println("Enter the number of frames : "); nof=s.nextInt(); for(i=0;i<nof;i++) { frames[i]=-1; fs[i]=0; } System.out.println("Enter the number of pages : "); nop=s.nextInt();; int pages[]=new int[10]; System.out.println("Enter the pages : "); for(i=0;i<nop;i++) { pages[i]=s.nextInt();; } while(l<nop) { flag1=0;flag2=0; for(int k=0;k<nof;k++) { if(pages[l]==frames[k]) { hit++; flag1=1; flag2=1; break; } } if(flag1==0) { for(i=0;i<nof;i++) { if(frames[i]==-1) { frames[i]=pages[l]; miss++; flag2=1; break; } } } if(flag2==0) { for(i=0;i<nof;i++) fs[i]=0; for(m=l-1,n=1;n<nof;n++,m--) { for(i=0;i<nof;i++) { if(frames[i]==pages[m]) fs[i]=1; } } for(i=0;i<nof;i++) { if(fs[i]==0) index= i; } frames[index]=pages[l]; miss++; } l=l+1; display(); } } static void display() { for(i=0;i<nof;i++) { System.out.println("\n"+frames[i]); } System.out.println("\nMiss : "+miss+"\n\n\n"); } } /* OUTPUT D:\SECMPN 60>javac LRU.java D:\SECMPN 60>java LRU Enter the number of frames : 3 Enter the number of pages : 7 Enter the pages : 1 2 4 1 3 5 6 1 -1 -1 Miss : 1 1 2 -1 Miss : 2 1 2 4 Miss : 3 1 2 4 Miss : 3 1 3 4 Miss : 4 1 3 5 Miss : 5 6 3 5 Miss :…

    • 303 Words
    • 2 Pages
    Good Essays
  • Better Essays

    The following program, ArrayDemo, creates an array of integers, puts some values in it, and prints each value to standard output.…

    • 1094 Words
    • 5 Pages
    Better Essays

Related Topics