Jump to content

[Request] Help with codes


Recommended Posts

Hi,

I  need help with codes on pictures in attachments. I need it "now" cause I must pass the exam of programming who's I hate. Thanks for help!

 

http://i45.tinypic.com/35na1zm.jpg

http://i50.tinypic.com/14wgm13.jpg

 

 

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

I  need help with codes on pictures in attachments. I need it "now" cause I must pass the exam of programming who's I hate. Thanks for help!

 

http://i45.tinypic.com/35na1zm.jpg

http://i50.tinypic.com/14wgm13.jpg

 

we dont understand this language, translate exercises in english.

Link to comment
Share on other sites

  • 2 weeks later...

They are wrote in polish language, i know its been a while but maybe u will learn something:

1. What variables will print on the screen below fragment of the program?

int e[][]={{45,5,6},{7,9,4},{8,8,9}};
for(int x[]:e) for(int y:[x]) System.out.print(y);

ok so in first line we have got 2 dimensional table, first[] before e, are all variables inside {}, so e[1] will give us {7,9,4}, but we always need to write something in 2nd [], which means number inside {}.

e[0][1] will give us 5, e[2][2] will give us 9.

for(int x[]:e) - thats something harder, we have got a loop which will create for us 3, 1 dimensioned tables, so first we will have int[] x = {45,5,6}; later x will be {7,9,4} and at the end {8,8,9}; so if we would have something like this:

for(int x[]:e) System.out.print(x[0]);

4578 - would appear.

now for(int y:[ x ]) - that doesnt exist in java 1.7, it should be for(int y:x) - loop will create y variable for each number in the table.

So first (y=45,x=0), (y=5,x=0), (y=6,x=0), (y=7,x=1) etc

what we have at the last is System.out.print(y).

So we will have 4556794889 :)

 

2.Create new object of class B, run method and set any value to variable i

class A {int i=10 ; int metoda(){int b=10; return b; }}
class B extends A {int i ; int metoda(int t){byte b=10; return b+t;}}

B ob = new B(); - creating new object of class B

ob.i = 123; - setting any value to variable i

 

3.What implements function?

 void function(){ int n=10; int[][] tab=new int[n][];
for(int i=0;i<n;i++) tab[i]=new int[i+1];
System.out.println(tab.length); System.out.println(tab[n-1].length); }

So, int[][] tab=new int[n][]; - we are creating new 2 dimensional table, tab length will be now n(10)

for(int i=0;i<n;i++) tab[i]=new int[i+1];

tab[0] length will be 1, tab[1] length will be 2 etc.

System.out.println(tab.length); - will give us tab length = 10

System.out.println(tab[n-1].length); - will give us length of tab[9], it will be 10

 

4.What will be printed on the screen?

 void function(){ int n=4; for(int i=n;i>1;i--){ for(int j=0;j<i;j++)
System.out.print(" "); System.out.println("*");} }

for(int i=n;i>1;i--)

- this loop will work 3 times, 4>1, 3>1 and 2>1, in each case, 2nd loop will start

for(int j=0;j<i;j++)

- when i=4, this will work 4 times(j=0, j=1, j=2 and j=3), each time j is increased, to the screen is added " ", and each time i is decreased, "*" and skipping to next line is printed so it will be like this:

    *
   *
  *

 

5. What will appear on the screen?

public class rozne {
enum wyliczenie {Syrenka(500), Fiat125p, OpelCorsa(25000), Yaris(26000), Laguna(45000);
private int cena;
wyliczenie(int cena){this.cena=cena;}
wyliczenie(){ this.cena=-1;}
int cena()Preturn cena;}
}
public static void main(String[] args) {
for(wyliczenie x: wyliczenie.values())
odpowiedz(x);
}
static void odpowiedz(wyliczenie ob)
{
switch(ob)
{
case Syrenka : System.out.print("\t Samochod ="+ob);
case Fiat125p : System.out.print("\t Samochod ="+ob);break;
case Laguna : System.out.print("\t Samochod ="+ob); break;
}}}

Ok so, this is runned first:

for(wyliczenie x: wyliczenie.values())

so each value of enum Wyliczenie will be in variable x, first x=Syrenka, 2nd x=Fiat125p etc

each time method odpowiedz is runned, we are going to switch - putting our x(now ob) inside and we are checking if ob=Syrenka later if ob=Fiat125p and at the end if ob=Laguna

so, at the beggining ob is Syranka, so we are printing \t - thats Tabulator, Samochod=Syrenka.

But hey, we dont have a break after that, so its not being closed, we are printing it 2nd time, now our text will be like this:

	 Samochod =Syrenka	 	 Samochod=Syrenka

later into odpowiedz, we are putting Fiat125p, it is printed only once since after System.out.print is break;

we are not printing OpelCorsa because we dont have anything like that in switch

at the end it will be like this:

 Samochod = Syrenka	 Samochod = Syrenka	 Samochod = Fiat125p	 Samochod = Laguna

 

6. What's wrong with the code?

class A{int a; public A(int i){a=4*i;}
void pokaz(){System.out.print(a);}}
class B extends A{int b; public B(int i, int j){super(i); b=2*j;}}
public class Main{ public static void main(String[] args){
B ob = new B(3,5); A oa; A o=new A(3); oa.pokaz();}}

Whats wrong with the code? hmm or isnt initialized, u cannot use oa.pokaz() on null or not yet initialized reference :)

 

7. What will appear in the screen?

class A{int a=1; int b=2; public A(int i,int j){}
public A(A o){a=2*o.a; b=3*o.b;} void pokaz(){System.out.println(a+"_"+b);} }
public class Main { public static void main(String[] args) {
A oa= new A(0,0);A ob= new A(oa);A oc= new A(ob);oc.pokaz(); }}

ok so first, this is runned: A oa= new A(0,0); - we are creating new object, i=0, j=0 but inside {} we have got nothing, so we are going to next line:

A ob= new A(oa); - now we have got a=2*o.a(1) so a=2, b=3*o.b(2), so b=6

A oc= new A(ob); - same thing, a=2*2=4, b*3*6=18

oc.pokaz(); - "4_18" will be printed on the screen :)

 

Hope that i helped somebody :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...