For this project, you will create a program that asks the user to enter a positi
ID: 3668373 • Letter: F
Question
For this project, you will create a program that asks the user to enter a positive integer value less than 20. If the user enters a number greater than 20, the user should get an error. If the user enters a number equal to or less than 20, display the double of each value beginning with 1 up to the selected number (multiply each number by 2), then provide the total of all doubles.
For example, if the user entered the number 5, the following should display:
Double up 1 = 2
Double up 2 = 4
Double up 3 = 6
Double up 4 = 8
Double up 5 = 10
Total = 30
Minimum Requirements:
Create a class that has 3 public functions.
A mutator function should take the number from the user and store it in a private variable.
Another function should perform the calculations, display the double up results and store the total in a private variable. This function must use a loop.
An accessor function should display the total.
Create a main function that creates an object of your class and calls each of the functions of the class.
Explanation / Answer
class Doubleup
{
private int number;
private int total;
public void getNum(int no)
{
number=no;
}
public int doubleup(int num)
{
for(int i=1;i<=num;i++)
{
System. out.println("doubleup "+i+ (i+i));
total+=i+i;
}
}
public void display()
{
System. out.println(" Total="+total);
}
}
class DoubleupDemo
{
public static void main(String[] args)
{
Doubleup d= new Doubleup();
Scanner sc= new Scanner( System.in));
System. out.println("enter any number");
int no=sc.nextInt();
if(no>20)
System.out.println("error");
else
{
d.getNum(no);
d.doubleup(no);
d.display ();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.