Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Error when reading/writing boolean value with JOption*

$
0
0
I am trying to read in parameters of a triangle then display them in JOptionPane. All of the parameters are working except the boolean value filled and the color. i want to try and solve the boolean issue first. My test program is as follows and under that are the two class files i created. i am getting the following error:
TestTriangle.java:31: error: incompatible types
           filled = true;
                    ^
  required: int
  found:    boolean


//java 2
//15-2
//by taddei
// test new triangle class

import javax.swing.JOptionPane;
public class TestTriangle {
  public static void main(String[] args) {
  
  JOptionPane.showMessageDialog(null,"You will be asked to enter triangle parameters.\n" + 
		"Then the program will display triagle details");
		
		////get triangle parameters////
		
	String side1String = JOptionPane.showInputDialog(null,"Enter side A length: ");
		double side1 = Double.parseDouble(side1String);
	String side2String = JOptionPane.showInputDialog(null,"Enter side B length: ");
		double side2 = Double.parseDouble(side2String);
	String side3String = JOptionPane.showInputDialog(null,"Enter side C length: ");
		double side3 = Double.parseDouble(side3String);
	
	///does not read color
	String colorString = JOptionPane.showInputDialog(null,"Enter color: ");
		String color = colorString;
	
	int filled = JOptionPane.showConfirmDialog(null,
                                 "Is the triangle filled? ", "Please select",
                                 JOptionPane.YES_NO_OPTION);
        
        if(filled==1) 
           filled = true;
        else
          if(filled==0)
             filled = false; 
	
		
	
		
    TriangleNew triangle = new TriangleNew(side1, side2, side3);
    
	 triangle.setColor("yellow");
    //triangle.setFilled(true);

    JOptionPane.showMessageDialog(null, "The area is: " + triangle.getArea()+
	 "\nThe perimeter is: " + triangle.getPerimeter() + "\nThe color is: " +
	 triangle.getColor() + "\nTriangle is filled: " + triangle.isFilled());
	 
	 
  }
}



//java 2
//15-2
//by taddei
//new triangle sub class extends geo obj


import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;


  class TriangleNew extends GeometricObject {
  private double side1 = 1.0, side2 = 1.0, side3 = 1.0;

  /** Constructor */
  public TriangleNew() {
  }

  /** Constructor */
  public TriangleNew(double side1, double side2, double side3) {
    this.side1 = side1;
    this.side2 = side2;
    this.side3 = side3;
  }

  /** Implement the abstract method findArea in GeometricObject */
  public double getArea() {
    double s = (side1 + side2 + side3) / 2;
    return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
  }

  /** Implement the abstract method findCircumference in
   * GeometricObject
   **/
  public double getPerimeter() {
    return side1 + side2 + side3;
  }

  @Override
  public String toString() {
    // Implement it to return the three sides
    return "TriangleNew: side1 = " + side1 + " side2 = " + side2 +
      " side3 = " + side3;
  }
}


//java 2
//15-2
//by taddei
//geometric object abstract class

public abstract class GeometricObject {
	private String color  = "white";
	private boolean filled;
	private java.util.Date dateCreated;
	
	//construct default object
	
	protected GeometricObject() {
		dateCreated = new java.util.Date();
		
		}
		
		///construct geo ob w/ color and fill val
		
		protected GeometricObject(String color, boolean filled){
			dateCreated = new java.util.Date();
			this.color = color;
			this.filled = filled;
			
		}
		
		//return col
		public String getColor(){
		return color;
		
	}
	
	//set new col
	public void setColor(String color){
		this.color = color;
	}	
		//return filled
		
	public boolean isFilled(){
		return filled;
		
	}
	//set new fill
	public void setFilled(boolean filled){
	
	}
	//get date
	public java.util.Date getDateCreated(){
		return dateCreated;
	}
	
	//return string rep
	
	public String toString(){
		return "created on " + dateCreated + "\ncolor: " + color +
		 " and filled: " + filled;
	}
	
	///abs meth get area
	public abstract double getArea();
	///abs get perim
	public abstract double getPerimeter();
	}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>