Jump to content

Who can solve these programs in Java??

- - - - -

  • Please log in to reply
9 replies to this topic

#1
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
hi everybody
kindly Who can solve these programs in Java?? :crying::crying:

1-Write a java program that reads and calculates the sum of integer numbers(using while loop)(JOptionPane(input).
Note :the input (-1) signifies the end of the input.

2-Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computer the total and average of the input values, not counting zeros. Your program ends with the input 0. Display the average as a floating-point number.(For example, if you entered 1,2 and 0 , the average should be 1.5) (JOptionPane or Scanner(input))


3-(Conversion from miles to kilometers) write a program that displays the following table (note that 1 mile is 1.609 kilometers)(JOptionPane or Scanner(input):

Miles......Kilometers
1...........1.609
2........... 3.218
.
.
.
9............14.481
10..........16.09




Please Please help meeeee:crying:

Edited by Ebda3, 13 April 2011 - 09:57 PM.


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Moved to the correct forum (you did not post a tutorial).

What do you have so far?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
I am sorry to put it in the wrong forum Pro...

actually these 3 programs that u read it how we can resole it ?

Thanx for your interesting

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
We can help you figure out how to do it if you give us what you have written so far and let us know where you are having trouble with it.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
Just follow this psuedocode, it will help you. I only gave the psuedocode for one and two because
three follows the similar pattern of 1 and 2.

PROGRAM 1:

   begin program


      declare a sum x


      while input != -1


         ask user for y

         store input in y

         x += y


      end while


      display x 

   END


PROGRAM 2:

   BEGIN program

      declare a average x

      declare a sum y

      declare a counter i

   

      while input != 0


         ask user for z

         store input in z

         y += z


         ++i


      end while


      x = y / i

      display x


   END 


#6
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Thank you all for your cooperation

Dear gregwarner
:love:

Dear mr mike, :thumbup:

how do you see this figure for a program 1??



Quote

import javax.swing.*;
public class S1
{
public static void main(String args[])
{

int x=Integer.parseInt(JOptionPane.showInputDialog("Please enter num1 :"));

while(x!= -1)
{
int y=Integer.parseInt(JOptionPane.showInputDialog("Please enter num2 :"));

x+=y;
System.out.println(x);
}

}
}



Program 2

import javax.swing.*;
public class S2
{
public static void main(String args[])
{

int x;
int y;
int i;

int input=Integer.parseInt(JOptionPane.showInputDialog("Please enter num :"));

while(input!= 0)
{
int z=Integer.parseInt(JOptionPane.showInputDialog("Please enter num2 :"));

y+=z;
++i;

}

System.out.println("x ="+(y/i));

}
}

:crying:

about program 3 it doesn't clear to me

and I am grateful and appreciate your effort
:)

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Close, but in both program 1 and 2, you use 2 variables for input.
x and y in program 1. input and z in program 2.

And try to give your variables meaningful names like "input" in program 2. That means something, instead of x, y, .. (It's ok for loop counters)

You want 1 variable to be the input, 1 to be the sum/total. Like for 1:

int input=Integer.parseInt(JOptionPane.showInputDialog("Pl ease enter num1 :"));

int sum = 0;


while(input!= -1)

{

    sum  += input;

    input=Integer.parseInt(JOptionPane.showInputDialog("Pl ease enter num2 :"));

}

System.out.println("total: " + sum);


And 2: (note this won't compile due to the "???" for average and the if-statement.)

int average;

int total;

int positiveCount;

int i;


int input=Integer.parseInt(JOptionPane.showInputDialog ("Please enter num :"));


while(input!= 0)

{

    if(???) {

        positiveCount++;

    }

    total += input;

    input = Integer.parseInt(JOptionPane.showInputDialog("Pl ease enter num2 :"));

    ++i;

    average = ???;

    

}


System.out.println("Total: " + total);

System.out.println("Average: " + average);

System.out.println("Number of positives: " + positiveCount + ", number of negatives: " + (i-positiveCount) );



I find program 3 the easiest of them all. I recommend a for-loop. Then just loop from 1 to 10, and do 1.609 * loopNumber and print it.
Example for loop, loops 50 times:

for([B][COLOR="lime"]int i=0[/COLOR][/B] ;[B][COLOR="orange"] i<50[/COLOR][/B] ; [B][COLOR="red"]i++[/COLOR][/B]){

   System.out.println( i );

}

The green part gets executed 1 time, before starting the loop.
The orange part is executed every loop, before actually doing the loop.
The red part is executed at the end of every loop.

If written with a while loop, it would look like:

[B][COLOR="lime"]int i=0;[/COLOR][/B]

while( [B][COLOR="orange"]i<50[/COLOR][/B] ){

   System.out.println( i );

   [B][COLOR="red"]i++;[/COLOR][/B]

}

As you see, it's just an easier and prettier way of writing a loop.

#8
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Dear wim DC
actually i get the program1

Quote

import javax.swing.*;
public class S1
{
public static void main(String args[])
{

int input=Integer.parseInt(JOptionPane.showInputDialog("Pl ease enter num1 :"));
int sum = +1;


while(input!= -1)
{
input=Integer.parseInt(JOptionPane.showInputDialog("Pl ease enter num2 :"));

sum += input;

}
System.out.println("total: " + sum);

}
}

still 2 nd 3 im trying

#9
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Program 3

Quote

import javax.swing.*;
public class S2
{
public static void main(String args[])
{
System.out.print("Mile ");
System.out.println("\t\tKiloMeters ");

int input=Integer.parseInt(JOptionPane.showInputDialog ("Please enter num :"));
for(int i=1 ; i<=10 ; i++){

System.out.println( i );

}

The output doesn't look what i want

still the Kilometer how i can put it in the table

#10
Ebda3

Ebda3

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
I think this is the solution what should be like in Program 3

Quote

import javax.swing.*;
public class S2
{
public static void main(String args[])
{
System.out.print("Mile ");
System.out.println("\t\tKiloMeters ");

int input=Integer.parseInt(JOptionPane.showInputDialog ("Please enter num :"));
for(int i=1 ; i<=10;i++){

System.out.print(i);
System.out.println("\t\t "+i*1.609);

}
}
}


remaing Program 2
:confused:

who can explain it more :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users