In: Computer Science
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.
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 *********************************************************