[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to students. 10 pizzas are baked, then served to 20 students.
Students are numbered between 0 and 19 in the order they are served.
A message is printed indicating whether a student starved or was served a pizza.
(a.) Rewrite the DiningHall class so that after the makePizza( ) method is called 10 times,
the servePizza( ) method is called once each from 20 different threads.
(b.) Insert synchronization to eliminate data races in your code, if any exist.
(c.) Describe what data races may occur in your multithreaded code without synchronization.
public class DiningHall {
static int pizzaNum;
static int studentID;
public void makePizza() {
pizzaNum++;
}
public void servePizza() {
String result;
if (pizzaNum > 0) {
result = "Served ";
pizzaNum--;
} else {
result = "Starved ";
}
System.out.println(result + studentID);
studentID++;
}
public static void main(String[] args) {
DiningHall d = new DiningHall();
for (int i = 0; i < 10; i++) {
d.makePizza();
}
for (int i = 0; i < 20; i++) {
d.servePizza();
}
}
}
Copyable Code:
public class DiningHall extends Thread
{
static int pizzaNum;
static int studentID;
public void makePizza()
{
pizzaNum++;
}
public synchronized void run()
{
String result;
if (pizzaNum > 0)
{
result = "Served ";
pizzaNum--;
}
else
{
result = "Starved ";
}
if(studentID<20)
System.out.println(result + studentID);
studentID++;
}
public static void main(String[] args)
{
DiningHall d = new DiningHall();
for (int i = 0; i < 10; i++)
{
d.makePizza();
for (int j = 0; j < 20; j++)
{
new DiningHall().start();
}
}
}
}
[Java] [Threads] The following class implements a model of a student dining hall serving pizzas to...
I need code in java
The Student class:
CODE IN JAVA:
Student.java file:
public class Student {
private String name;
private double gpa;
private int idNumber;
public Student() {
this.name = "";
this.gpa = 0;
this.idNumber = 0;
}
public Student(String name, double gpa, int
idNumber) {
this.name = name;
this.gpa = gpa;
this.idNumber = idNumber;
}
public Student(Student s)...
Create an abstract Student class for Parker University. The
class contains fields for student ID number, last name, and annual
tuition. Include a constructor that requires parameters for the ID
number and name. Include get and set methods for each field; the
setTuition() method is abstract.
Create three Student subclasses named UndergraduateStudent,
GraduateStudent, and StudentAtLarge, each with a unique
setTuition() method. Tuition for an UndergraduateStudent is
$4,000 per semester, tuition for a GraduateStudent
is $6,000 per semester, and tuition for...
I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...
A teacher wants to create a list of students in her class. Using the existing Student class in this exercise. Create a static ArrayList called classList that adds a student to the classList whenever a new Student is created. In the constructor, you will have to add that Student to the ArrayList. Coding below was given to edit and use public class ClassListTester { public static void main(String[] args) { //You don't need to change anything here, but feel free...
Given a class called Student and a class called Course that
contains an ArrayList of Student. Write a method called
getDeansList() as described below. Refer to Student.java below to
learn what methods are available.
I will paste both of the simple .JAVA codes below
COURSE.JAVA
import java.util.*;
import java.io.*;
/******************************************************
* A list of students in a course
*****************************************************/
public class Course{
/** collection of Students */
private ArrayList<Student> roster;
/*****************************************************
Constructor for objects of class Course
*****************************************************/...
Writing 3 Java Classes for Student registration /** * A class which maintains basic information about an academic course. */ public class Course { /** * Attributes. */ private String code; private String title; private String dept; // name of department offering the course private int credits; /** * Constructor. */ public Course(String code, String title, int credits) { // TODO : initialize instance variables, use the static method defined in // Registrar to initialize the dept name variable...
Here is the code from the previous three steps:
#include <iostream>
using namespace std;
class Student
{
private:
//class variables
int ID;
string firstName,lastName;
public:
Student(int ID,string firstName,string lastName)
//constructor
{
this->ID=ID;
this->firstName=firstName;
this->lastName=lastName;
}
int getID() //getter method
{
return ID;
}
virtual string getType() = 0; //pure virtual function
virtual void printInfo() //virtual function to print basic details
of a student
{
cout << "Student type: " << getType() <<
endl;
cout << "Student ID: " << ID...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
PLEASE DO IN JAVA 3) Add the following method to College: 1. sort(): moves all students to the first positions of the array, and all faculties after that. As an example, let fn indicate faculty n and sn indicate student n. If the array contains s1|f1|f2|s2|s3|f3|s4, after invoking sort the array will contain s1|s2|s3|s4|f1|f2|f3 (this does not have to be done “in place”). Students and faculty are sorted by last name. You can use any number of auxiliary (private) methods, if needed....
The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...