Share on Google+Share on Google+

Rose Marie
Object Oriented Programming II
2 Answer(s)      6 years and 5 months ago
Posted in : Java Beginners

Instructions:

->each class to be created must have encapsulated fields, setters and getters methods, and constructors

  1. Create a class named "Paper" with the following attributes -"length" of type int -"width" of type int -"content" of type String

    provide a method to compute the area of the paper

  2. Create a class named "Envelope" with the following attribute

    • "compilation" which is an array of type Paper

    provide a method to compute the total area of all papers inside the envelope provide a method to compare if two envelopes are equal provide a method to determine if there are two same papers inside the envelope

Ads

View Answers

November 25, 2010 at 5:10 PM


Hi Friend,

Try the following code:

import java.util.*;
class  Paper{
    int length;
    int width;
    String content;

    Paper(int length,int width,String content){
    this.length=length;
    this.width=width;
    this.content=content;
    }
    public void setLength(int length){
        length=length;
    }
    public int getLength(){
        return length;
    }
    public void setWidth(int width){
        width=width;
    }
    public int getWidth(){
        return width;
    }
    public void setContent(String content){
        content=content;
    }
    public String getContent(){
        return content;
    }
}
    class Envelope{
    public static void main(String[] args){
        int ar1[]=new int[5];
        int ar2[]=new int[5];
        int count=0;
        int totalarea1=0,totalarea2=0;
        System.out.println("Envelope 1:");
        Paper p1[]=new Paper[5];
        Scanner input=new Scanner(System.in);
            for(int i=0;i<p1.length;i++){
            System.out.println("Enter length of paper "+(i+1)+": ");
            int len=input.nextInt();
            System.out.println("Enter width of paper "+(i+1)+": ");
            int wid=input.nextInt();
            int aa=len*wid;
            ar1[i]=aa;
            p1[i]=new Paper(len,wid,Integer.toString(aa));
            p1[i].setLength(len);
            p1[i].setWidth(wid);
            p1[i].setContent(Integer.toString(aa));
        }
        for(int i=0;i<p1.length;i++){
            totalarea1+=Integer.parseInt(p1[i].getContent());
        }
        System.out.println("Total Area of all papers inside the envelope1: "+totalarea1);
         System.out.println("Envelope 2:");
         Paper p2[]=new Paper[5];
            for(int i=0;i<p2.length;i++){
            System.out.println("Enter length of paper "+(i+1)+": ");
            int len=input.nextInt();
            System.out.println("Enter width of paper "+(i+1)+": ");
            int wid=input.nextInt();
            int aa=len*wid;
            ar2[i]=aa;
            p2[i]=new Paper(len,wid,Integer.toString(aa));
            p2[i].setLength(len);
            p2[i].setWidth(wid);
            p2[i].setContent(Integer.toString(aa));
        }
        for(int i=0;i<p2.length;i++){
        totalarea2+=Integer.parseInt(p2[i].getContent());
        }
        System.out.println("Total Area of all papers inside the envelope2: "+totalarea2);
        if(totalarea1==totalarea2){
            System.out.println("Envelopes are equal");
        }
        else{
            System.out.println("Envelopes are not equal");
        }
    }
}

Thanks



November 25, 2010 at 5:10 PM


Hi Friend,

Try the following code:

import java.util.*;
class  Paper{
    int length;
    int width;
    String content;

    Paper(int length,int width,String content){
    this.length=length;
    this.width=width;
    this.content=content;
    }
    public void setLength(int length){
        length=length;
    }
    public int getLength(){
        return length;
    }
    public void setWidth(int width){
        width=width;
    }
    public int getWidth(){
        return width;
    }
    public void setContent(String content){
        content=content;
    }
    public String getContent(){
        return content;
    }
}
    class Envelope{
    public static void main(String[] args){
        int ar1[]=new int[5];
        int ar2[]=new int[5];
        int count=0;
        int totalarea1=0,totalarea2=0;
        System.out.println("Envelope 1:");
        Paper p1[]=new Paper[5];
        Scanner input=new Scanner(System.in);
            for(int i=0;i<p1.length;i++){
            System.out.println("Enter length of paper "+(i+1)+": ");
            int len=input.nextInt();
            System.out.println("Enter width of paper "+(i+1)+": ");
            int wid=input.nextInt();
            int aa=len*wid;
            ar1[i]=aa;
            p1[i]=new Paper(len,wid,Integer.toString(aa));
            p1[i].setLength(len);
            p1[i].setWidth(wid);
            p1[i].setContent(Integer.toString(aa));
        }
        for(int i=0;i<p1.length;i++){
            totalarea1+=Integer.parseInt(p1[i].getContent());
        }
        System.out.println("Total Area of all papers inside the envelope1: "+totalarea1);
         System.out.println("Envelope 2:");
         Paper p2[]=new Paper[5];
            for(int i=0;i<p2.length;i++){
            System.out.println("Enter length of paper "+(i+1)+": ");
            int len=input.nextInt();
            System.out.println("Enter width of paper "+(i+1)+": ");
            int wid=input.nextInt();
            int aa=len*wid;
            ar2[i]=aa;
            p2[i]=new Paper(len,wid,Integer.toString(aa));
            p2[i].setLength(len);
            p2[i].setWidth(wid);
            p2[i].setContent(Integer.toString(aa));
        }
        for(int i=0;i<p2.length;i++){
        totalarea2+=Integer.parseInt(p2[i].getContent());
        }
        System.out.println("Total Area of all papers inside the envelope2: "+totalarea2);
        if(totalarea1==totalarea2){
            System.out.println("Envelopes are equal");
        }
        else{
            System.out.println("Envelopes are not equal");
        }
    }
}

Thanks










Related Tutorials/Questions & Answers:
Object Oriented Programming II
Object Oriented Programming II  Instructions: ->each class to be created must have encapsulated fields, setters and getters methods, and constructors Create a class named "Paper" with the following attributes
object oriented programming protocol
object oriented programming protocol  What is object oriented programming protocol? Is it different from objective c protocol programming?? Thanks
Advertisements
Object Oriented Programming in PHP
Object Oriented Programming in PHP  Hi, Which version of PHP supports OOPS. How can I learn Object Oriented Programming in PHP? Thanks
object oriented programming - Java Beginners
object oriented programming  sir, i read in the book tat object oriented program exhibits recurring structures. i want to know "what is meant by recurring structures?"  Hi Friend, Any structure to be occurred over
What are the features and advantages of OBJECT ORIENTED PROGRAMMING?
What are the features and advantages of OBJECT ORIENTED PROGRAMMING?  What are the features and advantages of OBJECT ORIENTED PROGRAMMING
Object-Oriented programming - Java Beginners
Object-Oriented programming  Write a program to display the names and salaries of 5 employees.Make use of a class and an array.The salary of each employee should increase by 5% and displayed back.  Hi friend, Code
Object Oriented Programming in Java
OOPS acronym for Object Oriented Programming is a model or concept that works around objects and data. Programming language like Java that follow the OOP... of a class. For a language to be an Object Oriented Programming Language, it must
Java using Netbeans Object Oriented Programming
Java using Netbeans Object Oriented Programming   I am trying to make a program that uses a main class and a separate class with one public method and one private method. The main method in the main class asks the user for 2
Object Oriented
Object Oriented  C++ is Purely object oriented or not .Then why java called purely object oriented
An Overview of Java      Java is a programming language      Java is Object Oriented Programming
; Java as a programming language Java is an Object oriented...; Java as an Object Oriented Language In this section, we will discuss the OOPs... are in pure C code, not C++. The function prototypes are in an object-oriented form
Is Java object oriented?
Is Java object oriented?  Hi, Is Java object oriented? Thanks   Hi, Java is object oriented programming language. It is supports OPPS concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation All
What is attribute oriented programming?
What is attribute oriented programming?  Hi, What is attribute oriented programming? Thanks
Is Java a pure object oriented language?
programming language but not purely a object oriented language. In OOPs programming... through the class and object creation. For more object oriented programming language...Is Java a pure object oriented language?   Hi, Is Java a pure
java is pure object oriented
java is pure object oriented  java is pure object oriented or not.? reason.?   Java is not pure object oriented language because... are not object 2)It does not support operator overloading multiple inheritance. 3
Exp.4 Write any C++ programs to demonstrate multiple inheritance concept of an object oriented programming.
Exp.4 Write any C++ programs to demonstrate multiple inheritance concept of an object oriented programming.  (Aim:- The main aim of this experiment is reusability of code and getting idea about classifying objects , identifying
java : object oriented
java : object oriented   (a) Write a class named CarRental... the user for the data needed for a rental and creates an object of the correct... an object of the correct type. Display the total rental fee
Object Oriented concepts
OOP stands for Object Oriented Programming. This is a technique used to develop programs revolving around the real world entities. In OOPs programming... on the following links Object Oriented concepts
Java Object
Java Object         Object is the basic entity of object oriented programming language. Object.../java-object-oriented-language.shtml  
Object oriented program - Java Beginners
Object oriented program  Write a class called RaceCar.java that contains instance variables horsepower, weight, zeroTo60, and milesPerGal. The value of zeroTo60 (in second) is determined by the formula zeroTo60 = weight
object oriented program.. - Java Beginners
object oriented program..  Write a class called RaceCar.java that contains instance variables horsepower, weight, zeroTo60, and milesPerGal. The value of zeroTo60 (in second) is determined by the formula zeroTo60 = weight
fully object oriented language - Java Beginners
fully object oriented language  Is java is a fully object oriented language?if no,why?  Hi Friend, Java is not a fully object oriented... int,float which are not object 2)It does not support operator overloading
Object oriented design - Java. - Design concepts & design patterns
Object oriented design - Java.  Hi, Everyone.Pls help me to develope my project of Object oriented design, as I have to submit it on thursday,3rd Dec. 2009.Nowadayz I am suffering from fever and not able to concentrate
About Java Programming Language
About Java Programming Language       Java is an Object oriented application programming language developed by Sun Microsystems. Java is a very powerful general-purpose programming
Object Orient Programming
Object Orient Programming   I have this program that needs to display multiple taxis. I have the code but there is an error. Could someone tell me where i am going wrong?? import java.awt.*; import javax.swing.*; public
core java code in object oriented manner - Java Interview Questions
core java code in object oriented manner   hi this is komali .i want small code for this question .its very urgent .please send me as soon as possible . i am waiting for code Sports Associations in India
Learning Java Programming for Beginners
Java is an Object Oriented Programming Language that was developed by Sun Microsystems. It is based on C, C++ programming language but a tad better... are eyeing the vast field of programming and software development
OOP
; Object Oriented Programming which was totally based on the real life entities... for Object oriented  Programming. Previously we used to follow the procedural programming paradigm while developing the software which makes it hard to add
Learn Java Programming
you proficient in the language. Java is an Object Oriented Programming(OOP... in it, Learn Java programming online with tutorials and simple examples... and mobile phones. Career in Java programming is vast and that is why most
PHP Class Object
PHP Class Object: In object oriented programming a class can be an abstract... apple, orange are the object of this class. Object is the instantiate... Object Class Example: <?php class A { public function disp(){ echo
Part II. Appendixes
Part II. AppendixesPrev   Next    AppendixesBibliography[EJB_2.0] Enterprise JavaBeansTM Specification, Version 2.0. [JavaRanch] JavaRanch.com forum for SCBCD Certification
Part II. Appendixes
Part II. AppendixesPrev   Next    AppendixesBibliography[WSAD_512_INFOCENTER] WebSphere Studio Application Developer information center - http://publib.boulder.ibm.com
Tutorials   
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? neo4j-server.properties file location SASLError using PLAIN: not-authorized

Ads

 
Advertisement null

Ads