1. Modify Astack class so that the program can process stack of class objects. T
ID: 3706667 • Letter: 1
Question
1. Modify Astack class so that the program can process stack of class objects. The definition of the class Student is provided as below: class Student private String name; private int id; private int grade; public int ID get f return id; ) set { id = value; } public void Input () Console.Write("Enter name:") name Console.ReadLine(); Console.Write("Enter ID:); id = int.Parse(Console.ReadLine()); Console.Write("Enter Grade:); grade - int.Parse(Console.ReadLine)); public override string ToString) return string. Format("Name: eID: 1), Grade: [2)name, id, grade); The following is a test running output nter st sica Lee i King ere is the current content of the statck: nter 1 to pop. 2 to push. 3 to exit The iten poped out is: Name: Bi11 King. ID: 1003. Grade: 90 The itens in the St ter i to pop. 2 to push. 3 to eit nter a st to push into the Stack: r wood ter pushing are:Explanation / Answer
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Stack
{
class Student
{
private string name;
private int id;
private int grade;
public int ID
{
get { return id; }
set { id = value; }
}
public int GRADE
{
get { return grade; }
set { grade = value; }
}
public string NAME
{
get { return name; }
set { name = value; }
}
public void Input()
{
Console.Write("Enter name: ");
name = Console.ReadLine();
Console.Write("Enter ID: ");
id = int.Parse(Console.ReadLine());
Console.Write("Enter Grade: ");
grade = int.Parse(Console.ReadLine());
}
public override string ToString()
{
return string.Format("Name: {0}, ID: {1}, Grade: {2} ", name, id, grade);
}
}
class AStack
{
Student[] arr;
int top;
int size;
public AStack(int Size)
{
arr = new Student[Size];
size = Size;
top = -1;
}
public bool Empty()
{
if (top == -1)
return true;
else
return false;
}
public bool Full()
{
if (top == size - 1)
return true;
else
return false;
}
// public void Push(int x)
public void Push(Student x)
{
if (!Full())
{
top = top + 1;
arr[top] = new Student();
arr[top].NAME = x.NAME;
arr[top].ID = x.ID;
arr[top].GRADE = x.GRADE;
}
else
Console.WriteLine("The stack is full! ");
}
public void Pop()
{
if (!Empty())
{
Student x = arr[top];
top = top - 1;
Console.WriteLine(" The item poped out is: Name: {0}, ID: {1}, Grade: {2}", x.NAME, x.ID, x.GRADE);
}
else
{
Console.WriteLine("The stack is empty! ");
}
}
public void Print()
{
if (top == -1)
Console.WriteLine("The stack is empty!");
else
for (int i = top; i >= 0; i--)
Console.WriteLine(" Name: {0}, ID: {1}, Grade: {2}", arr[i].NAME, arr[i].ID, arr[i].GRADE);
}
}
class Program
{
static void Main(string[] args)
{
int num, flag, p;
int numOfStudents, id, grade;
Student s = new Student();
string name;
AStack arrStack = new AStack(11);
Console.WriteLine("Enter number of student: ");
numOfStudents = int.Parse(Console.ReadLine());
for (int i = 0; i < numOfStudents; i++) //adding 10 initial values into the statck
{
Console.WriteLine("Enter student {0}: ", i + 1);
Console.Write("Enter name: ");
name = Console.ReadLine();
Console.Write("Enter ID: ");
id = int.Parse(Console.ReadLine());
Console.Write("Enter Grade: ");
grade = int.Parse(Console.ReadLine());
s.NAME = name;
s.ID = id;
s.GRADE = grade;
arrStack.Push(s);
}
Console.WriteLine("Here is the current content of the statck: ");
arrStack.Print();
Console.WriteLine(" Enter 1 to pop, 2 to push, 3 to exit");
flag = int.Parse(Console.ReadLine());
while (flag != 3)
{
if (flag == 1)
{
arrStack.Pop();
Console.WriteLine(" The items in the Stack after poping are:");
arrStack.Print();
}
else
{
if (flag == 2)
{
Console.WriteLine("Enter an student to push it into the Stack:");
Console.Write("Enter name: ");
name = Console.ReadLine();
Console.Write("Enter ID: ");
id = int.Parse(Console.ReadLine());
Console.Write("Enter Grade: ");
grade = int.Parse(Console.ReadLine());
s.NAME = name;
s.ID = id;
s.GRADE = grade;
arrStack.Push(s);
Console.WriteLine("The items in the Stack after pushing are:");
arrStack.Print();
}
}
Console.WriteLine("Enter 1 to pop, 2 to push, 3 to exit");
flag = int.Parse(Console.ReadLine());
}
Console.Read();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.