Directions...Write a program SumOfOdds that has one main method that asks the user to input two odd integers where the first is smaller than the second. The program ouputs the sum of all the odd integers from the smaller output to the larger output. You must use a while loop for this program.

public class SumOfOdds {

    public SumOfOdds(int input1, int input2)
    {
		num1 = input1;
		num2 = input2;
    }
    int num1;
    int num2;
    if (num1%2=1)&&(num2%2=1)
    	while (num1<num2)
    	{
    		int sum = sum
		}
	private int num1;
	private int num2;
}

Recommended Answers

All 6 Replies

Did you write this code ? I think there are way too many mistakes here. Write the class properly. There are repetitions of variables. Also use functions to write your code.

Probably then, we can think of writing the functionality that you want.

An example,

public class A{
	
	private int c;
	private int b;
	
	public void A(){
		
	}
	
	public void A(int x, int y){
		c = x;
		b = y;
	}

        public void myFunctions(){
               //do my functions.
        }
}

How is that...?

public class SumOfOdds {

    public void SumOfOdds(int input1, int input2)
    {
		num1 = input1;
		num2 = input2;
    }
    if (num1%2=1)&&(num2%2=1)
    	while (num1<num2)
    	{
    		int sum = sum
		}
	private int num1;
	private int num2;
}

It still is not correct. Add your code into a function.

private int num1;
	private int num2;

you should ideally be declaring these at the start of your class.

I have done that at the bottom... I set my instance fields.

OK. Add your code into a function. A class can have functions and variables. Put your code inside a function.

I altered it a smig. What should I implement now?

import javax.swing.JOptionPane;
public class SumOfOdds {

    public static void main (String[] args)
	{
		String number = JOptionPane.showInputDialog("Enter Number 1: ");
		double number = Double.parseDouble(input);
		String number = JOptionPane.showInputDialog("Enter a number that is greater than Number 1: ");
		double number = Double.parseDouble(input);
		SumOfOdds sum = new Sum(number);
    	while (num1<num2)
			if (num1%2==1)&&(num2%2==1)
    		{

    		}
		System.exit(0);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.