Question

E 4 (4 pts.Determine the output of the program helow. impon util. public class ForEachFun output private int public ForEach u

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is the code:

                import java.util.*;
                public class ForEachFun
                {
                        private int num;
                        public ForEachFun(int n){num=n;}
                        public void number(){num--;num/=5%2;}
                        public String toString(){return ""+num;}
                      
                        public static void main(String[] args)
                        {
                                ForEachFun [] m=new ForEachFun[4];
                                for(int i=0;i<m.length;i++)
                                if(i%3 ==1) m[i]=new ForEachFun(-5);
                                else
                                m[i]=new ForEachFun(8+i);

                                after these lines of code:

                                        //m[0]=8
                                        //m[1]=-5
                                        //m[2]=10
                                        //m[3]=11

                              
                                for(ForEachFun n:m)
                                n.number();

                                after these lines of code:

                                here's a close look what number() function is doing, basically it is only decreementing
                                value of num because 5%2=1 and any number devided by 1 is number itself
                                        //m[0]=8-1=7
                                        //m[1]=-5-1=-6
                                        //m[2]=10-1=9
                                        //m[3]=11-1=10
                                for(ForEachFun n:m)
                                System.out.println(n);
                        }
                }


final answer:

7
-6
9
10

7 -6 10

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
E 4 (4 pts.Determine the output of the program helow. impon util. public class ForEachFun output private int public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the output of this program? class A { public int i; private int j;...

    What is the output of this program? class A { public int i; private int j; } class B extends A { void display() { super.j = super.i + 1; System.out.println(super.i + " " + super.j); } } class Inheritance { public static void main(String args[]) { B obj = new B(); obj.i=1; obj.j=2; obj.display(); } } Java language!! // include explanation!

  • What is the output of the following program? public class Contacts Contact contacts; private void...

    What is the output of the following program? public class Contacts Contact contacts; private void addContact Contact b) if( contactsnull) contactsb; else Contact tempcontacts; contacts b; b.nexttemp; private void showContactList) Contact tempcontacts; while( tempnull) f System.out.print( temp.name + ", " ); temp temp.next; public static void main( String [] args) Contacts myContacts new Contacts ) myContacts.addContact( new Contact("Peter")); myContacts.addContact( new Contact( "Paul"); myContacts.addContact( new Contact("Mary"); myContacts.showContactList); class Contact public String name; public Contact next; public Contact( String t ) f...

  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

  • public class Test { private static int i =0; private static int j =0; public static...

    public class Test { private static int i =0; private static int j =0; public static void main(String[] args) { int i = 2; int k = 3; { int j =3; System.out.println("i + j is : " + i + j); } k = i + j; System.out.println("K is " + k ); System.out.println("K is " + j); } } why is the output i + j = 23 K =2 K =0 Please explain a step by step...

  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary...

    public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...

  • class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i...

    class Upper { private int i; private String name; public Upper(int i){ name = "Upper"; this.i = i;} public void set(Upper n){ i = n.show();} public int show(){return i;} } class Middle extends Upper { private int j; private String name; public Middle(int i){ super(i+1); name = "Middle"; this.j = i;} public void set(Upper n){ j = n.show();} public int show(){return j;} } class Lower extends Middle { private int i; private String name; public Lower(int i){ super(i+1); name =...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT