Getting Started Create a directory called prac0 (in your cpl directory) and make
ID: 3742267 • Letter: G
Question
Getting Started Create a directory called prac0 (in your cpl directory) and make t your current working directory All programs will be created from seratch and so there are no files to copy. Start jGrasp and be prepared to write complete class definitions. Task 1 Copy and paste (or type in) the following class definitons into jGrasp elass FrogPrograml ( public statie void main (String args) ( Frog myFrog new Frog O; /create a Frog objeet my Frog name Fred my Frog. print ) ) I end of main l II end of FrogPrograml elass Frog String naneNamenotset int id-111 -1 used to indieates it has not been set Frog 1 Constructor currently does nothing // no code in body of constructor void print) System,out printin(Start of print method System out,printin (Frog'sname is+ name) System.out printin(Id isid): Save the file as FrogProgram1.java Compile and run the a pplication. It should produce the outpu Start of print method Frog' nane 1a Fred Id is -1 29
Explanation / Answer
class FrogProgram1
{
public static void main (String[] args)
{
Frog myFrog = new Frog();
myFrog.name = "Fred";
myFrog.id = 1000;
myFrog.print();
}
}
class Frog
{
String name = "Name not set";
int id = -1;
Frog()
{
}
void print()
{
System.out.println("**** Start of print method ********");
System.out.println("Frog's name is "+ name);
System.out.println("Id is "+id);
}
}
Output:
**** Start of print method ********
Frog's name is Fred
Id is 1000
Task 2:
class FrogProgram2
{
public static void main (String[] args)
{
Frog myFrog = new Frog();
myFrog.name = "Fred";
myFrog.id = 1000;
myFrog.age = 22;
myFrog.print();
myFrog.printAgeGroup(); // call to printAgeGroup() method
}
}
class Frog
{
String name = "Name not set";
int id = -1;
int age = 0;
Frog() // default constructor
{
}
void print()
{
System.out.println("**** Start of print method ********");
System.out.println("Frog's name is "+ name);
System.out.println("Id is "+id);
}
void printAgeGroup()
{
// print age group according to age
if(age >=0 && age <=20)
System.out.println("Age Group is young");
if(age >=21 && age <=50)
System.out.println("Age Group is juvenile");
if(age >50)
System.out.println("Age Group is adult");
}
}
Output:
**** Start of print method ********
Frog's name is Fred
Id is 1000
Age Group is juvenile
Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.