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();
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();
}
}
Hello, I ran into some issues with my code and wanted to ask if anyone can...
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 | |...
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...
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 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...
Create a base class and two derived classes. Also, create and call a member function named communicate() that behaves differently when called by each class. Create a single C++ project (and CPP source file) named animal_communication as follows: Into this source, type the source code for Program 15-16, "Program Output," in Section 15.6, "Polymorphism and Virtual Member Functions," in Ch. 15, "Inheritance, Polymorphism, and Virtual Functions," of Starting Out With C++ From Control Structures Through Objects. Run and debug the...
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{...
previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() { //create Animal object Animal animal("Dog","Mammal",4); cout<<"Animal object details"<<endl; cout<<"Name: "<<animal.getName()<<endl; cout<<"Type: "<<animal.getType()<<endl; cout<<"# of Legs: "<<animal.getLegs()<<endl; cout<<endl<<endl; //create Cat object Cat cat("byssinian cat","carnivorous mammal",4,"short...
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...
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...
The goal of this homework is to write a small database system for an Animal Shelter. This is a no-kill shelter like the one just west of the Addition Airport. You will accept animal “donations” to the shelter, but mostly only dogs and cats. (But yes, there may be the occasional hamster or other nondog, non-cat animal donation.) At present, all we need to do is write the skeleton of a database that will track all the animals in the...