Menu

[Solved]Convert Python Code Java S Python Source Code Animal Class Class Animal Final String Whole Q37274012

How do I convert this python code to java?

Here’s the python source code (Animal class):

class Animal:
#final string for whole string
final_string = “”
#constructor and setting the values
def __init__(self, name, gut):
self.name = name
self.gut = gut

#greet method
def greet(self):
print (“Hello my name is ” + self.name + “.”)
#I am user inputting the eatables of animals for reference
self.gut = input(“enter the animal eatables: “).split()

#printing the animal:name
def __str__(self):
#if length is zero, return empty. return animal:name is digestingnothing
if len(self.gut) == 0:
return “Animal: ” + self.name + ” is digesting nothing.”
else:
string = “Animal: ” + self.name + ” is digesting “
for i in range(len(self.gut)):
”’
i == len(self.gut)-2: if current element is last but oneelement,
then add “and” to end of string (like worm AND fly)
i == len(self.gut)-1: if current element is last element,
then no need to add anything as it is the last eatable addfullstop.
else:
add comma at end of string
”’
if i == len(self.gut) – 2:
string += self.gut[i] + ” and “
elif i == len(self.gut) – 1:
string += self.gut[i] + “.”
else:
string += self.gut[i] + “,”
return string

#empty list for gut
gut = []
#object creation and sending parameters
p1 = Animal(“Froggy”, gut)
#calling greet method
p1.greet()
print(p1.__str__())

Expert Answer


Answer to How do I convert this python code to java? Here’s the python source code (Animal class): class Animal: #final string for… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *