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 2: Explain what you think will be displayed to the screen in Step 1. (Reference: For…

    • 365 Words
    • 2 Pages
    Satisfactory Essays
  • Good Essays

    It210 Week 4 Psuedocode

    • 584 Words
    • 3 Pages

    Write: “Enter 6 if you would like to Quit now.” Input Display_Selection While Display_Selection ! = 1 || 2|| 3 || 4…

    • 584 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    System.out.println("i = " + i + " f = " + f + " d = " + d);…

    • 2338 Words
    • 10 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
  • Better Essays

    Program Lovecs.Java

    • 422 Words
    • 2 Pages

    import java.util. Scanner; public class LoveCS { public static void main(String[] args) { Scanner scan=new Scanner(System.in); System.out.println("Enter how many times you wish the message to be printed."); int limit = 0; limit= scan.nextInt(); int sum=0; int count=1; while (count <=limit) { System.out.println(count+" I love Computer Science!!"); sum+=count; count++; } System.out.println("Printed this message " + limit + " times."); System.out.println("The sum of the numbers from 1 to "+limit+" is "+sum); } } import java.util. Scanner; public class PowersOf2 { public static void main(String[]args) { int valuePowersOf2; int nextPowersOf2 =1; int exponent= 0; int count=0; Scanner scan = new Scanner(System.in); System.out.println("Enter A Number.");…

    • 422 Words
    • 2 Pages
    Better 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

    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

    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

    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
  • 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
  • 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