Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:36:37+00:00 2026-05-26T08:36:37+00:00

I am working on my homework assignment and I am completely stuck! What I

  • 0

I am working on my homework assignment and I am completely stuck! What I am trying to do is to use already defined input and save it to the file by using saveDataTo() method and read the input by using readDataFrom() method.

I am stuck on the first part. I am not sure if I have to initialize the data in Program.cs file first?

I don’t know and I am stuck. Here is code and hope for some tips how I can accomplish this.

— EDIT —

I can add instructions for purpose of both saveDataTo() and readDataFrom() methods:

The saveDataTo( ) method takes a parameter of BinaryWriter. The method
writes the values of all 5 properties of an book object to a file
stream associated with the writer (the association is done in the
Main( ) method of Program class). There is no need to open and close
the file stream and binary writer inside this method.

The readDataFrom( ) method takes a parameter of BinaryReader. The
method reads the values of all five properties of the Book object from
a file stream associated with the reader (the association is done in
the Main( ) method of Program class). There is no need to open and
close the file stream and binary reader inside this method.

So that gives me a clue that I should use and assign the properties to be saved in the file there?

— EDIT —

Updated the code there. I do have a problem with content that is being saved into the file. I am not being showed the price. Why is that?

ff.APublisherNameTitle  FirstNameLastName

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Program
    {
        private const string FILE_NAME = "lab07.dat";

        static void Main(string[] args)
        {
            //char ask;

            /*
            do
            {
                Console.Write("Enter Book Title: ");
                publication.Title = Console.ReadLine();
                Console.Write("Enter Author's First Name: ");
                book.AuthorFirstName = Console.ReadLine();
                Console.Write("Enter Author's Last Name: ");
                book.AuthorLastName = Console.ReadLine();
                Console.Write("Enter Publisher's Name: ");
                publication.PublisherName = Console.ReadLine();
                Console.Write("Enter Book Price: $");
                publication.Price = float.Parse(Console.ReadLine());
                Console.Write("Would like to enter another book? [Y or N] ");
                ask = char.Parse(Console.ReadLine().ToUpper());
            }
            while (ask == char.Parse("Y"));
            */

            Book book = new Book();

            book.Price = 10.9F;
            book.Title = "Title";
            book.PublisherName = "PublisherName";
            book.AuthorFirstName = "FirstName";
            book.AuthorLastName = "LastName";

            FileStream fileStream = new FileStream(FILE_NAME, FileMode.OpenOrCreate);
            BinaryWriter write = new BinaryWriter(fileStream);
            book.saveDataTo(write);
            write.Close();

            fileStream = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
            BinaryReader read = new BinaryReader(fileStream);
            book.readDataFrom(read);
            read.Close();
        }
    }
}

Publication.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Publication
    {
        private float price;
        private string publisherName, title;

        public float Price
        {
            get
            {
                return price;
            }
            set
            {
                price = value;
            }
        }

        public string PublisherName
        {
            get
            {
                return publisherName;
            }
            set
            {
                publisherName = value;
            }
        }

        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public void display()
        {
            Console.WriteLine("{0}\n{1}\n{2}", title, publisherName, price);
        }
    }
}

Book.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Lab_7
{
    class Book : Publication
    {
        private string authorFirstName, authorLastName;

        public string AuthorFirstName
        {
            get
            {
                return authorFirstName;
            }
            set
            {
                authorFirstName = value;
            }
        }

        public string AuthorLastName
        {
            get
            {
                return authorLastName;
            }
            set
            {
                authorLastName = value;
            }
        }

        public new void display()
        {
        }

        public string getAuthorName()
        {
            return authorFirstName + " " + authorLastName;
        }

        public void readDataFrom(BinaryReader r)
        {
            Price = r.ReadInt32();
            PublisherName = r.ReadString();
            Title = r.ReadString();
            authorFirstName = r.ReadString();
            authorLastName = r.ReadString();
        }

        public void saveDataTo(BinaryWriter w)
        {
            w.Write(base.Price);
            w.Write(base.PublisherName);
            w.Write(base.Title);
            w.Write(AuthorFirstName);
            w.Write(AuthorLastName);
        }
    }
}

Regards.

HelpNeeder.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:36:37+00:00Added an answer on May 26, 2026 at 8:36 am

    You assign your parameters to 2 different objects, see:

    Publication publication = new Publication(); 
    Book book = new Book(); 
    

    Both are individual instances residing in memory.

    You either have to refer the publication to the book like:

    Book book = new Book(); 
    Publication publication = (Publication)book;
    

    or just assign the values currently assigned to the publication directly to the book so:

    publication.PublisherName = "PublisherName"; 
    

    becomes

    book.PublisherName = "PublisherName"; 
    

    Apart from that, you’re working in C#, not Java. By convention its normal to start your methods with a Capital (Pascal Case)

    EDIT

    Your now shown the price when reaidng since you write it as a floating field (or double, cant see the definition) and read it as an integer.

    Change from r.ReadInt32(); to r.ReadDouble(); or r.ReadSingle()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a homework assignment in which I'm required to use char arrays
I am working on this homework assignment and I am stuck on what I
I am working on a homework assignment and I'm trying to figure out a
I'm working on a homework assignment to modify code given by my professor using
I am currently working on a homework assignment and I am thoroughly stuck. I
I'm working on a homework assignment in Perl CGI using the CGI.pm module. In
Ok, so I am working on a homework assignment, and I am using SWING
I am working on a homework assignment where we aren't allowed to use any
I'm working on Polynomial Transform for a homework assignment. I'm using a document from
For a homework assignment, we are working on CSV parser. I'm trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.