Question

Assignment 1. Console Application IN PYTHON For this assignment you are to create a class called...

Assignment 1. Console Application IN PYTHON

For this assignment you are to create a class called Lab1. Inside the class you will create three methods that will modify a three-digit whole number:

sumNums - This method takes as input a three digit int and returns the sum of the three numbers. For instance, 123 would return 6 because 3 + 2 + 1 is 6

reverseNums - This method takes as input a three digit whole number and returns the number in reverse as a String. For instance, 123 would return "321"

getList - This method takes as an argument a three digit whole number and returns the numbers as an int list. Each element of the list should contain one of the numbers

In the main method prompt for a three digit number to be input.

  1. Create an instance of the Lab1 class
  2. Call the sumNums method passing to it the whole number that was input into the Console window
  3. Call the reverseNums method passing to it the whole number that was input into the Console window.
  4. Call the getList method passing the whole number that was input at the Console window. Print the list out one number per line from the Main method using a for loop.

Note:

You can use a for loop to iterate through a list as follows:

myList = [2,4,6,8,10]

for nums in myList:
    print(nums)

Your output should be the sum of the digits, the whole number in reverse order as a String, and the int as a list.

Your output should resemble the following:

Things to Review

  • for loops
  • counted loops with while
  • lists
  • Value producing methods

Things that will cost you points

  • Converting the three-digit number to a string to get each individual character. You should use modulus and divide.
  • print in any method of the class
  • Using input in any method of the class.

Homework Answers

Answer #1

Have a look at the below code. I have put comments wherever required for better understanding.

class Lab1:
  # create the constructor
  def __init__(self,number):
    self.number = number 
  # create sum function
  def sumNums(self):
   # take variable to store sum
    sum = 0 

    copy = self.number
    # loop while numer is greater than 0
    while (copy>0):
      # get the last digit
      rem = copy%10 
      # divide number by 10
      copy = copy//10
      # sum the digit
      sum+=rem 
    # return sum
    return sum 

  def reverseNums(self):
    # take variable to store reverse number
    rev = ""

    copy = self.number
    # loop while numer is greater than 0
    while (copy>0):
      # get the last digit
      rem = copy%10 
      # divide number by 10
      copy = copy//10
      # add it to string variable
      rev+=str(rem)
    # return reverse
    return rev 


  def getList(self):
    # take empty list to store digits
    lst = []
    
    copy = self.number
    # loop while numer is greater than 0
    while (copy>0):
       # get the last digit
      rem = copy%10 
      # divide number by 10
      copy = copy//10
      # add it to the list
      lst.append(rem)
    # return list
    return lst[::-1] 

# take user input
number = int(input("Enter the three digit number: "))
# create object
obj = Lab1(number)
# call each method
print(obj.sumNums())
print(obj.reverseNums())
print(obj.getList())








  

  

  

Happy Learning!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Questions: 1. (5 marks) Create a VB.NET Console Application that defines a function Smallest and calls...
Questions: 1. Create a VB.NET Console Application that defines a function Smallest and calls this function from the main program. The function Smallest takes three parameters, all of the Integer data type, and returns the value of the smallest among the three parameters. The main program should (1) Prompt a message (using Console.WriteLine) to ask the user to input three integers. (2) Call the built-in function Console.ReadLine() three times to get the user’s input. (3) Convert the user’s input from...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...
1. let us create a python class called Star(). Write Star() such that an instance (s1)...
1. let us create a python class called Star(). Write Star() such that an instance (s1) of a star is created with the call s1=Star(…) where the calling parameters of Star() define all the information of the star, including, at least, name, distance, surface temperature and radius. Look these up for your favorite star and run a main that proves it works. 2. Next, create a second class called Information(). It will have zero calling parameters and just one item...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT