Question

Hello, I ran into some issues with my code and wanted to ask if anyone can...

Hello,

I ran into some issues with my code and wanted to ask if anyone can help me fix them. Thank you in advance!

Question:

You and your team of software developers are experiencing problems saving the state of a program between different times that the program is run.

An object oriented program state can be maintained by saving the state of the objects in the program. Also, you can transfer the state of an object between different computer systems by creating the object based on the state once it is saved onto that other computer. In order to provide a way to persist object state, you must serialize objects in C#.

As a team of software developers and using the classes created in the Week Four Learning Team Collaborative Activity, "Learn How to Apply Inheritance Using Class Hierarchies," serialize the objects of the classes from that activity to a file as a Visual Studio® solution.

Code:

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Week5_Project
{

public class Animal
{
static void Main()
{
Serialize();
Deserialize();
}

}
{
private int age;
private int weight;
private int height;
}   
// default constructor
public Animal(int age, int weight, int height)
{
this.age = age;
this.weight = weight;
this.height = height;
}
  
public class Cat : Animal
{
public Cat() { }
public Cat(int age, int weight, int height) : base(age, weight, height)
{
}
}
public class Dog : Animal
{
public Dog() { }
public Dog(int age, int weight, int height) : base(age, weight, height)
{
}
}
public class Bird : Animal;
{
public Bird() { }
public Bird(int age, int weight, int height) : base(age, weight, height)
{
}
}
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(int age, int weight, int height);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Week5_Project
{
private int age;
private int weight;
private int height;

public class Animal
{
// default constructor

public Animal(int age, int weight, int height)

{
this.age = age;
this.weight = weight;
this.height = height;
}
static void Main()
{
Serialize();
Deserialize();
}


public void Serialize() {
ClassToSerialize c = new ClassToSerialize();
File file = new File("data.dat");
Stream stream = file .Open(FileMode.Create);
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(stream , c);
stream .Close();
}


public void Deserialize() {
ClassToSerialize c = new ClassToSerialize();
File file = new File("data.dat");
Stream stream = file .Open(FileMode.Open);
BinaryFormatter binary = new BinaryFormatter();
c = (ClassToSerialize) binary .Deserialize(s);
Console.WriteLine(c.name);
stream.Close();
}

}
  


public class Cat : Animal
{
public Cat() { }
public Cat(int age, int weight, int height) : base(age, weight, height)
{
this.age = age;
this.weight = weight;
this.height = height;
}
}
public class Dog : Animal
{
   public Dog() { }
   public Dog(int age, int weight, int height) : base(age, weight, height)
   {
       this.age = age;
       this.weight = weight;
       this.height = height;
   }
}
public class Bird : Animal;
{
   public Bird() { }
   public Bird(int age, int weight, int height) : base(age, weight, height)
   {
       this.age = age;
       this.weight = weight;
       this.height = height;
   }
}
BinaryFormatter formatter = new BinaryFormatter();
try
{
formatter.Serialize(int age, int weight, int height);
}
catch (SerializationException e)
{
Console.WriteLine("Failed to serialize. Reason: " + e.Message);
throw;
}
finally
{
fs.Close();
}
}

Add a comment
Know the answer?
Add Answer to:
Hello, I ran into some issues with my code and wanted to ask if anyone can...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C# - Inheritance exercise I could not firgure out why my code was not working. I...

    C# - Inheritance exercise I could not firgure out why my code was not working. I was hoping someone could do it so i can see where i went wrong. STEP 1: Start a new C# Console Application project and rename its main class Program to ZooPark. Along with the ZooPark class, you need to create an Animal class. The ZooPark class is where you will create the animal objects and print out the details to the console. Add the...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height;...

    Given the following code: #include <iostream> #include <iomanip> using namespace std; class Animal{ private: int height; int weight; public: void setHeight(int h){ height = h; } int getHeight(){ return height; } void setWeight(int w){ weight = w; } int getWeight(){ return weight; } virtual void makeSound() = 0; virtual void eat(); }; class Dog: public Animal{ public: void makeSound(){ cout << "Bow Bow\n"; } void eat (){ cout <<"The dog is eating\n"; } void Catch(){ cout << "The dog is...

  • In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to...

    In Exercise 1, displayAnimal uses an Animal reference variable as its parameter. Change your code to make the displayAnimal function anim parameter as an object variable, not a reference variable. Run the code and examine the output. What has changed? Polymorphic behavior is not possible when an object is passed by value. Even though printClassName is declared virtual, static binding still takes place because anim is not a reference variable or a pointer. Alternatively we could have used an Animal...

  • C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement...

    C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...

  • I have completed a large portion of this work... but I don't know if it is...

    I have completed a large portion of this work... but I don't know if it is good enough. Please look it over and give me any advice, tips, critiques, etc. I think I may need more constructors. Here are the directions to this portion: Before you begin modifying and creating classes, your team lead reminds you to demonstrate industry standard best practices in all your code to ensure clarity, consistency, and efficiency among all software developers working on the program....

  • Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare...

    Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare an abstract class FarmAnimalBase that implements the interface FarmAnimal. In the class FarmAnimalBase, define a private final instance variable name (type: String), a public constructor that initializes the value of the instance variable name, and a public method getName() that returns the value of the instance variable name. Note that we do not implement the method talk() declared in the interface FarmAnimal, that’s the...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • I have done a decent amount of coding... but I need you to help me understand...

    I have done a decent amount of coding... but I need you to help me understand what I do not. Please, post comments so that I can learn from you. Here are the instructions for this portion of the project: Create the Monkey class, using the specification document as a guide. The Monkey class must do the following: Inherit from the RescueAnimal class Implement all attributes with appropriate data structures Include accessors and mutators for all implemented attributes Here is...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT