Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Problems I. Solve the following programming problems in Java language . Pro

ID: 3835720 • Letter: J

Question

Java Problems

I. Solve the following programming problems in Java language. Provide the code and a screenshot of a test run of the code.

1. Modify the payroll system of chapter 10 to include the following:

i. Include an additional Employee subclass named PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise produced. The class has private variables wage (the employee’s wage per piece) and pieces (number of pieces produced). Implement the earnings method in the class that multiplies the wage per piece produced.

ii. Create a test program with an array list of type Employee that holds an instance of each type of Employee.

2. An interface is defined below. Define an abstract class named Philosopher that implements the setters, getters and only the sleep() method. Two classes, Mathematician and Physicist, extend the Philosopher class and provide the implementation for the unimplemented methods. Test the implementations.

public interface Person {

void setFirstName(String firstName);

void setLastName(String lastName);

String getFirstName();

String getLastName();

void eat();

void think();

void sleep();

}

3. Design a program that merges the content of two text files containing chemical elements sorted by atomic number and produces a sorted file of elements. The program should read the content from the two files, and outputs the data ordered by atomic number to the output file. Assume the name of the output file. Use the provided files. The format of each file will be as following:

ATOMIC_NUMBER

ELEMENT_NAME

ELEMENT_ABBREVIATION

ATOMIC_WEIGHT

chems:

Explanation / Answer

Employee.java

public class Employee {

        private String name;

        private int age;

       

        Employee() {

                        this.name = "";

                        this.age = 0;

        }

       

       

        public Employee(String name, int age) {

                        this.name = name;

                        this.age = age;

        }

        public String getName() {

                        return name;

        }

        public void setName(String name) {

                        this.name = name;

        }

        public int getAge() {

                        return age;

        }

        public void setAge(int age) {

                        this.age = age;

        }

       

       

}

Pieceworker.java

public class PieceWorker extends Employee {

        private int wage;

        private int pieces;

        public PieceWorker(String name, int age, int wage, int pieces) {

                        super(name, age);

                        this.wage = wage;

                        this.pieces = pieces;

        }

        public int getWage() {

                        return wage;

        }

        public void setWage(int wage) {

                        this.wage = wage;

        }

        public int getPieces() {

                        return pieces;

        }

        public void setPieces(int pieces) {

                        this.pieces = pieces;

        }

        public int getEarnings() {// method to count the wages

                        return this.wage * this.pieces;

        }

        @Override

        public String toString() { // method to print the details

                        return "PieceWorker [Name :" + getName() + ", Age:" + getAge() + ", wage : " + wage + ", pieces=" + pieces

                                                        + ", Earnings=" + getEarnings() + "]";

        }

}

EmployeeDriver.java

import java.util.ArrayList;

import java.util.Iterator;

public class EmployeeDriver {

        public static void main(String[] args) {

                        ArrayList<Employee> employees = new ArrayList<>();

                       

                        PieceWorker wr = new PieceWorker("Sanjay", 24, 20, 3);

                        employees.add(wr);

                        wr = new PieceWorker("Rishi", 22, 34, 3);

                        employees.add(wr);

                        wr = new PieceWorker("Ricky", 22, 14, 20);

                        employees.add(wr);

                       

                        Iterator<Employee> i = employees.iterator();

                        PieceWorker e;

                        while(i.hasNext()) {

                                        e = (PieceWorker) i.next();

                                        System.out.println(e);

                        }

        }

}

Sample Run: -

PieceWorker [Name :Sanjay, Age:24, wage : 20, pieces=3, Earnings=60]

PieceWorker [Name :Rishi, Age:22, wage : 34, pieces=3, Earnings=102]

PieceWorker [Name :Ricky, Age:22, wage : 14, pieces=20, Earnings=280]

Person.java

public interface Person {

                void setFirstName(String firstName);

                void setLastName(String lastName);

                String getFirstName();

                String getLastName();

                void eat();

                void think();

                void sleep();

}

Philospher.java

public abstract class Philosopher implements Person{

       private String firstName;

       private String lastName;

       public String getFirstName() {

              return firstName;

       }

       public void setFirstName(String firstName) {

              this.firstName = firstName;

       }

       public String getLastName() {

              return lastName;

       }

       public void setLastName(String lastName) {

              this.lastName = lastName;

       }

      

       public void sleep() {

              System.out.println("Philospher Sleeps...");

       }

       @Override

       public String toString() {

              return "Philosopher [firstName=" + firstName + ", lastName=" + lastName + "]";

       }

      

      

}

Mathematician.java

public class Mathematician extends Philosopher{

       @Override

       public void eat() {

              System.out.println("Mathematician Eats ... ");

             

       }

       @Override

       public void think() {

              System.out.println("Mathematician thinks..");

             

       }

      

}

Physicist.java

public class Physicist extends Philosopher{

       @Override

       public void eat() {

              System.out.println("Physicist eats...");

             

       }

       @Override

       public void think() {

              System.out.println("Physicts thinks...");

             

       }

}

PersonTest.java

public class PersonTest {

       public static void main(String[] args) {

              Philosopher p = new Mathematician();

              p.setFirstName("Akash");

              p.setLastName("Srivastav");

              System.out.println(p);

              p.eat();

              p.think();

              p.sleep();

             

              Philosopher p1 = new Physicist();

              p1.setFirstName("Sanjay");

              p1.setLastName("Singh");

              System.out.println(p1);

              p1.eat();

              p1.think();

              p1.sleep();

       }

}

Sample Run: -

Philosopher [firstName=Akash, lastName=Srivastav]

Mathematician Eats ...

Mathematician thinks..

Philospher Sleeps...

Philosopher [firstName=Sanjay, lastName=Singh]

Physicist eats...

Physicts thinks...

Philospher Sleeps...

3.I am assuming your file is already merged as given, and I just need to parse it into the program and sort it.

Element.java

public class Element implements Comparable<Element>{

       private int atomicNum;

       private String elementName;

       private String elementAbbr;

       private double atomicWeight;

      

      

       public Element(int atomicNum, String elementName, String elementAbbr, double atomicWeight) {

              super();

              this.atomicNum = atomicNum;

              this.elementName = elementName;

              this.elementAbbr = elementAbbr;

              this.atomicWeight = atomicWeight;

       }

       public int getAtomicNum() {

              return atomicNum;

       }

       public void setAtomicNum(int atomicNum) {

              this.atomicNum = atomicNum;

       }

       public String getElementName() {

              return elementName;

       }

       public void setElementName(String elementName) {

              this.elementName = elementName;

       }

       public String getElementAbbr() {

              return elementAbbr;

       }

       public void setElementAbbr(String elementAbbr) {

              this.elementAbbr = elementAbbr;

       }

       public double getAtomicWeight() {

              return atomicWeight;

       }

       public void setAtomicWeight(double atomicWeight) {

              this.atomicWeight = atomicWeight;

       }

       @Override

       public int compareTo(Element o) {

             

              if(this.getAtomicNum() < o.getAtomicNum()) {

                     return -1;

              } else if(this.getAtomicNum() > o.getAtomicNum()) {

                     return 1;

              } else {

                     return 0;

              }

       }

      

      

}

ParseElement.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.PrintStream;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Iterator;

import java.util.Scanner;

public class ParseElements {

       public static void main(String[] args) {

              Scanner sc;

              ArrayList<Element> elList = new ArrayList<>();

              try {

                     sc = new Scanner(new File("chems.txt"));

                     while (sc.hasNext()) {

                           elList.add(new Element(Integer.parseInt(sc.nextLine()), sc.nextLine(), sc.nextLine(),

                                         Double.parseDouble(sc.nextLine())));

                     }

                    

                     Collections.sort(elList);

                    

                     PrintStream out = new PrintStream(new FileOutputStream("output.txt"));

                     System.setOut(out);

                    

                     Iterator<Element> el = elList.iterator();

                     Element e;

                     while(el.hasNext()) {

                           e = el.next();

                           System.out.println(e.getAtomicNum());

                           System.out.println(e.getElementName());

                           System.out.println(e.getElementAbbr());

                           System.out.println(e.getAtomicWeight());

                           System.out.println();

                     }

                    

              } catch (FileNotFoundException e) {

                     e.printStackTrace();

              }

       }

}

Output.txt

1

Hydrogen

H

1.01

2

Helium

He

4.0

3

Lithium

Li

6.94

4

Beryllium

Be

9.01

5

Boron

B

10.81

6

Carbon

C

12.01

7

Nitrogen

N

14.01

8

Oxygen

O

16.0

9

Fluorine

F

19.0

10

Neon

Ne

20.18

11

Sodium

Na

22.99

12

Magnesium

Mg

24.31

13

Aluminium

Al

26.98

14

Silicon

Si

28.09

15

Phosphorus

P

30.97

16

Sulfur

S

32.06

17

Chlorine

Cl

35.45

18

Argon

Ar

39.95

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote