In: Computer Science
You are going to create a console based program to keep track of a small business that sells Doodads.
First you will need to create a class Doodad that keeps track of two integers and two Strings.
Next, create a constructor for the Doodad.
Next, add getters and setters for each of the fields (two integers and two Strings).
You need to use Doodad.java (see the starter code)
Inside your main method ask the user to read in the two integers and two Strings. Create a Doodad object and add it to an array of 100 Doodad objects. Allow the user to keep entering in Doodad objects until the user wishes to quit.
Once the user is done entering Doodad objects, show the information about the Doodads they entered.
You will show the first number, followed by a space, followed by the first word.
Answer: Hey dear student finds the solution of your query, if you have any doubt feel free to ask. Thanks!!
C++ Code: this code had Doodad class has two integer type data members ,two String type data members. Class has a parametrized constructor. In Main, read values for integers and strings and create array of object Doodad class and passing the values to set data members of the class , ask from user if he wants to exit from input and print all entered values.
Any query asks in the comment box.
import java.io.*;
import java.util.*;
class Doodad//Doodad class
{
int a;//members of the class
int b;
String str1;
String str2;
//constructor of the class with parameters
public Doodad(int x,String s1,int y,String s2)
{
a = x;//initialization of
members
b = y;
str1 = s1;
str2 = s2;
}
public void setInt1(int x)//setter to set data member
of the class
{
a = x;
}
public void setInt2(int y)//setter to set data member
of the class
{
b = y;
}
public void setStr1(String str)//setter to set data
member of the class
{
str1 = str;
}
public void setStr2(String strr)//setter to set data
member of the class
{
str2 = strr;
}
//getters to get value of data members
public int getInt1()
{
return a;
}
public int getInt2()
{
return b;
}
public String getStr1()
{
return str1;
}
public String getStr2()
{
return str2;
}
}
//Main class
class Main
{
public static void main(String args[])//Main
method
{
int a1 = 0,b1,c=0,ans =1;
String st1,st2;//needed
variables
Scanner in = new
Scanner(System.in);//Scanner object
Scanner sc = new
Scanner(System.in);
Doodad []dd = new
Doodad[100];//create object of the class
while(ans>0)
{
//input statements to take integers and strings
System.out.println("Enter an
integer: ");
a1 =
in.nextInt();
System.out.println("Enter an integer: ");
b1 =
in.nextInt();
System.out.println("Enter a string: ");
st1 =
sc.nextLine();
System.out.println("Enter a string: ");
st2 =
sc.nextLine();
//call
constructors with passing parameters
dd[c] = new
Doodad(a1,st1,b1,st2);
c =c+1;
System.out.println("Enter -1 to exit");
ans =
in.nextInt();//sentinel value ask
}
for(int i = 0;i<c;i++)//loop to
print information that user has entered
{
System.out.print(dd[i].getInt1()+" ");//call all getters
System.out.print(dd[i].getStr1()+" ");
System.out.print(dd[i].getInt2()+" ");
System.out.println(dd[i].getStr2()+" ");
}
}
}
Screenshots of the output: