Question

In: Computer Science

Using Java This is two-part question, but I have already completed the first part and just...

Using Java

This is two-part question, but I have already completed the first part and just need help with the second. Here I will provide both questions and my answer to the first part:

Part I Question:

Write a class called Dog that contains instance data that represents the dog’s name, breed, weight, birthdate, and medical history. Define the Dog constructor to accept and initialize instance data (begin the medical history with an empty line). Include accessor and mutator methods every attribute. For the medical history in particular, define the mutator method to simply add strings to the medical history so the user sees a printout of information about the dog (be mindful of adding new line characters). Include a toString method that returns the dog's information in a "nice" format (I leave this up to you). Create a driver class called Vet, whose main method instantiates and updates several Dog objects.

Answer:

class Dog{
String name,breed,birth_date,medical_history="\n";
int weight;
public Dog(String n,String b,String d,String m,int w){//constructor of class
this.name=n;
this.breed=b;
this.birth_date=d;
this.weight=w;
this.medical_history+=("\n"+m);//new line character is used here for medical_history
}
public String getName(){
return this.name;
}
public String getBreed(){//getter and setter methods
return this.breed;
}
public String getBirthDate(){
return this.birth_date;
}
public String getMedicalHistory(){
return this.medical_history;
}
public int getWeight(){
return this.weight;
}
public void setName(String n){
this.name=n;
}
public void getBreed(String d){
this.breed=d;
}
public void getBirthDate(String d){
this.birth_date=d;
}
public void getMedicalHistory(String m){
this.medical_history+=("\n"+m);
}
public void setWeight(int w){
this.weight=w;
}
public String toString(){//string representation of Dog class
return "Name is: "+this.name+" Breed is: "+this.breed+" birth date is: "+this.birth_date+" Weight is: "+this.weight+" Medical History: "+this.medical_history;
}
}
public class Vet
{
   public static void main(String[] args) {
       Dog d1=new Dog("name1","breed1","date1","Healthy",20);//create a dog object and print it
       System.out.println(d1);
       d1.setName("Pinky");//update the name of dog and print the dog details
       System.out.println(d1);
   }
}

Part II ( graphical) Question:

Write a class called BarChart that compares the data using a bar graph representation. Allow the parameters to the constructor to be a set of 5 numbers (corresponding to different heights of the chart). Every bar in the graph must have a fixed width. Then, create a separate program whose main method draws bars of random height.

Solutions

Expert Solution

PART I

Dog.java

public class Dog {
private String name, breed, birth_date, medical_history;
private int weight;
  
public Dog()
{
this.name = this.breed = this.birth_date = "";
this.medical_history = "\n";
this.weight = 0;
}

public Dog(String name, String breed, String birth_date, String medical_history, int weight) {
this.name = name;
this.breed = breed;
this.birth_date = birth_date;
this.medical_history = "\n" + medical_history;
this.weight = weight;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getBreed() {
return breed;
}

public void setBreed(String breed) {
this.breed = breed;
}

public String getBirth_date() {
return birth_date;
}

public void setBirth_date(String birth_date) {
this.birth_date = birth_date;
}

public String getMedical_history() {
return medical_history;
}

public void setMedical_history(String medical_history) {
this.medical_history = medical_history;
}

public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}
  
@Override
public String toString()
{
return "Name is: " + this.name
+ " Breed is: " + this.breed
+ " birth date is: " + this.birth_date
+ " Weight is: " + this.weight
+ " Medical History: " + this.medical_history;
}
}

Vet.java (Main class)

public class Vet {
  
public static void main(String[] args) {
Dog d1 = new Dog("name1", "breed1", "date1", "Healthy", 20);//create a dog object and print it
System.out.println("DOG 1:\n------\n" + d1);
d1.setName("Pinky");//update the name of dog and print the dog details
d1.setBreed("Golden Retriever");
System.out.println(d1);
  
Dog d2 = new Dog("Tommy", "Doberman", "12/06/2018", "Sick", 17);//create a dog object and print it
System.out.println("\nDOG 2:\n------\n" + d2);
d2.setBreed("German Shepherd");
d2.setWeight(22);
System.out.println(d2);
}
}

SCREENSHOT :

***********************************************************************************************************************************

PART II

AxesPanel.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;

public class AxesPanel extends JPanel {
private int height1, height2, height3, height4, height5;
  
private static final int RECT_X = 100;
private static final int RECT_Y = RECT_X;
private static final int RECT_WIDTH = 300;
private static final int RECT_HEIGHT = RECT_WIDTH;
  
public AxesPanel()
{
this.height1 = 0;
this.height2 = 0;
this.height3 = 0;
this.height4 = 0;
this.height5 = 0;
}

public AxesPanel(int height1, int height2, int height3, int height4, int height5) {
this.height1 = height1;
this.height2 = height2;
this.height3 = height3;
this.height4 = height4;
this.height5 = height5;
}
  
private Color determineColor(int height)
{
Color res = Color.WHITE;
if(height <= -1 && height >= -40) // 1 - 20
res = Color.ORANGE;
else if(height <= -41 && height >= -100) // 21 - 40
res = Color.GRAY;
else if(height <= -101 && height >= -130) // 41 - 60
res = Color.YELLOW;
else if(height <= -131 && height >= -220) // 61 - 80
res = Color.BLUE;
else if(height <= -221 && height >= -280) // 81 - 100
res = Color.RED;
return res;
}
  
private void draw(Graphics g, int x, int y, int width, int height)
{
g.setColor(determineColor(height));
g.fillRect(x, y, width, height);
}
  
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.drawLine(-450, 300, 500, 300); // x-axis
g.drawLine(50, -330, 50, 330); // y-axis
// the dividers on the y-axis
g.drawString("10 __", 20, 280);
g.drawString("20 __", 20, 250);
g.drawString("30 __", 20, 220);
g.drawString("40 __", 20, 190);
g.drawString("50 __", 20, 160);
g.drawString("60 __", 20, 130);
g.drawString("70 __", 20, 100);
g.drawString("80 __", 20, 70);
g.drawString("90 __", 20, 40);
g.drawString("100 __", 20, 10);
  
// dividers on the x-axis
String horBar = "|";
g.drawString(horBar, 130, 305);
g.drawString("v1", 130, 320);
g.drawString(horBar, 210, 305);
g.drawString("v2", 210, 320);
g.drawString(horBar, 290, 305);
g.drawString("v3", 290, 320);
g.drawString(horBar, 370, 305);
g.drawString("v4", 370, 320);
g.drawString(horBar, 450, 305);
g.drawString("v5", 450, 320);
  
// 1 div on y-axis = 5
if(height1 != 0 && height2 != 0 && height3 != 0 && height4 != 0 && height5 != 0)
{
draw(g, 122, 300, 20, (getScaledHeight(height1) * -1));
draw(g, 202, 300, 20, (getScaledHeight(height2) * -1));
draw(g, 282, 300, 20, (getScaledHeight(height3) * -1));
draw(g, 362, 300, 20, (getScaledHeight(height4) * -1));
draw(g, 442, 300, 20, (getScaledHeight(height5) * -1));
}
}
  
private int getScaledHeight(int height)
{
int res = 0;
if(height >= 1 && height <= 10)
res = (0 + (height - 1) * 2);
else if(height >= 11 && height <= 20)
res = (20 + (height - 10) * 2);
else if(height >= 21 && height <= 30)
res = (50 + (height - 20) * 2);
else if(height >= 31 && height <= 40)
res = (80 + (height - 30) * 2);
else if(height >= 41 && height <= 50)
res = (110 + (height - 40) * 2);
else if(height >= 51 && height <= 60)
res = (140 + (height - 50) * 2);
else if(height >= 61 && height <= 70)
res = (170 + (height - 60) * 2);
else if(height >= 71 && height <= 80)
res = (200 + (height - 70) * 2);
else if(height >= 81 && height <= 90)
res = (230 + (height - 80) * 2);
else if(height >= 91 && height <= 100)
res = (260 + (height - 90) * 2);
return res;
}
  
@Override
public Dimension getPreferredSize()
{
return new Dimension(RECT_WIDTH + 2 * RECT_X, RECT_HEIGHT + 2 * RECT_Y);
}
}

BarChartMain.java (Main class)

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BarChartMain {
  
private static JFrame mainFrame;
private static JPanel mainPanel, centerPanel, rightPanel;
private static JLabel v1Label, v2Label, v3Label, v4Label, v5Label;
private static JTextField v1Field, v2Field, v3Field, v4Field, v5Field;
private static JButton okButton;
private static AxesPanel axesPanel;
  
public static void main(String[] args) {
mainFrame = new JFrame("Bar Chart");
mainPanel = new JPanel(new BorderLayout());
  
rightPanel = new JPanel(new GridLayout(6, 0));
JPanel p1 = new JPanel(new GridLayout(1, 2));
p1.setLayout(new GridBagLayout());
v1Label = new JLabel("v1: ");
v1Field = new JTextField(7);
p1.add(v1Label);
p1.add(v1Field);
JPanel p2 = new JPanel(new GridLayout(1, 2));
p2.setLayout(new GridBagLayout());
v2Label = new JLabel("v2: ");
v2Field = new JTextField(7);
p2.add(v2Label);
p2.add(v2Field);
JPanel p3 = new JPanel(new GridLayout(1, 2));
p3.setLayout(new GridBagLayout());
v3Label = new JLabel("v3: ");
v3Field = new JTextField(7);
p3.add(v3Label);
p3.add(v3Field);
JPanel p4 = new JPanel(new GridLayout(1, 2));
p4.setLayout(new GridBagLayout());
v4Label = new JLabel("v4: ");
v4Field = new JTextField(7);
p4.add(v4Label);
p4.add(v4Field);
JPanel p5 = new JPanel(new GridLayout(1, 2));
p5.setLayout(new GridBagLayout());
v5Label = new JLabel("v5: ");
v5Field = new JTextField(7);
p5.add(v5Label);
p5.add(v5Field);
JPanel p6 = new JPanel(new FlowLayout(FlowLayout.CENTER));
okButton = new JButton("Ok");
p6.add(okButton);
  
centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
axesPanel = new AxesPanel();
centerPanel.add(axesPanel);
  
rightPanel.add(p1);
rightPanel.add(p2);
rightPanel.add(p3);
rightPanel.add(p4);
rightPanel.add(p5);
rightPanel.add(p6);
  
mainPanel.add(rightPanel, BorderLayout.EAST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
  
mainFrame.add(mainPanel);
mainFrame.setSize(750, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLocationRelativeTo(null);
mainFrame.setVisible(true);
  
// action listener for ok button
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(v1Field.getText().equals("") || v2Field.getText().equals("") || v3Field.getText().equals("") || v4Field.getText().equals("") || v5Field.getText().equals(""))
{
JOptionPane.showMessageDialog(null, "All fields are mandatory!");
return;
}
int v1Height = Integer.parseInt(v1Field.getText().trim());
int v2Height = Integer.parseInt(v2Field.getText().trim());
int v3Height = Integer.parseInt(v3Field.getText().trim());
int v4Height = Integer.parseInt(v4Field.getText().trim());
int v5Height = Integer.parseInt(v5Field.getText().trim());
axesPanel = new AxesPanel(v1Height, v2Height, v3Height, v4Height, v5Height);
centerPanel.removeAll();
centerPanel.add(axesPanel);
mainPanel.add(centerPanel, BorderLayout.CENTER);
centerPanel.revalidate();
centerPanel.repaint();
}
});
}
}

******************************************************* SCREENSHOT *********************************************************


Related Solutions

I NEED PART 2 COMPLETED I ALREADY COMPLETED 1A-D COURSE PROJECT 1 INSTRUCTIONS You have just...
I NEED PART 2 COMPLETED I ALREADY COMPLETED 1A-D COURSE PROJECT 1 INSTRUCTIONS You have just been contracted as a new management trainee by Earrings Unlimited, a distributor of earrings to various retail outlets across the country. In the past, the company has done very little in the way of budgeting and at certain times of the year has experienced a shortage of cash. Since you are well trained in budgeting, you have decided to prepare a master budget for...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
I have figured out the first part of this question in that there is no significant...
I have figured out the first part of this question in that there is no significant difference because 1.78<2.306. But this second part is confusing to me. Can you explain the formula I need to use? Thanks .Menstrual cycle lengths (days) in an SRS of nine women are as follows: {31, 28, 26, 24, 29, 33, 25, 26, 28}. Use this data to test whether mean menstrual cycle length differs significantly from a lunar month using a one sample t-test....
Minerals question i have named all 16 minerals already i just need the function Major or...
Minerals question i have named all 16 minerals already i just need the function Major or trace ,Food Source,Name of deficiency or symptoms of deficiency,and toxicity and yes or No answer. There will be a total of 16 minerals (7 major & 9 trace) Minerals Name – both name and number if given Function Major or trace if minerals 4 food sources Name of deficiency or symptoms of deficiency Toxicity Yes or no 1.Calcium 2.Magnesium 3.Potassium 4.Sodium 5.Sulfer 6.Phosphorous 7.Chloride...
second part of the previous question already i have posted Income statement for Mars for 2018...
second part of the previous question already i have posted Income statement for Mars for 2018 and 2019 follows: Particulars 2019 ($) 2018 ($) Sales 250,000 180,000 Cost of Goods sold 140,000 110,000 Income before taxes 25000 25000 Income Tax expenses 4000 3000 Selling expenses 20,000 19,000 Interest expenses 3,500 4,500 Compute the ratios from the given balance sheet extract for 2018. Assets $ Cash 30,000 Marketable securities 17,000 Accounts receivables 12,000 Stock 10,000 Land and Building 200,000 Accumulated depreciation...
QUESTION 3 (12 marks) Part A: Ranier Ltd. has just completed its first year of operations...
QUESTION 3 Part A: Ranier Ltd. has just completed its first year of operations on December 31, 20X1. Net income for the year was $570. During the year, equipment costing $800 was purchased when the company paid cash of $640 and issued common shares worth $160. Near the end of the year, equipment costing $60 with accumulated depreciation of $16 was sold for $54. At the end of the year, accounts receivable was $250, accounts payable was $36 and accumulated...
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
I already answered the first three questions and put the answer down I just don't know...
I already answered the first three questions and put the answer down I just don't know how to answer the last question which asks to calculate K in the rate law? A clock reaction is run at 20 ºC with several different mixtures of iodide, sodium bromate and acid, to form iodine. Thiosulfate is used to react with the iodine formed initially. Starch indicator is added to form a blue color when all the thiosulfate has been used up and...
I have completed the first 5 parts just need 6&7 Need to complete it by noon...
I have completed the first 5 parts just need 6&7 Need to complete it by noon pacific time Thanks Problem 6-23 (Algo) Make or Buy Decision [LO6-3] Silven Industries, which manufactures and sells a highly successful line of summer lotions and insect repellents, has decided to diversify in order to stabilize sales throughout the year. A natural area for the company to consider is the production of winter lotions and creams to prevent dry and chapped skin. After considerable research,...
*********Please do not answer the question partially. I have already done step #1, now just needing...
*********Please do not answer the question partially. I have already done step #1, now just needing to complete all of the other steps (2-10)  an explantion included would be nice as well, I would appriciate it! :) *If you can not help completely or entirely; to the furthest extent please do not leave an answer******* Question 1        Required: #1. Prepare journal entries to record the December transactions in the General Journal Tab in the excel template file "Accounting Cycle Excel Template.xlsx"....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT