Home Answers Viewqa Java-Beginners Object Oriented Programming II

 
 


Rose Marie
Object Oriented Programming II
2 Answer(s)      2 years and 6 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

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 Pages:
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
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 Implementation of Numerical Methods - Java Tutorials
Object-Oriented Implementation of Numerical Methods 2002-04-01 The Java Specialists' Newsletter [Issue 044] - Review: Object-Oriented Implementation... - Vince Sabio) on my distribution list. Review: Object-Oriented
Object-Oriented Design - Overview
best practice is use of object-oriented approaches, using Object-Oriented Design (OOD) and implementing the design with Object-Oriented Programming (OOP... Java: Object-Oriented Design - Overview Background. Software development
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 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 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
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
Object-Oriented Programming
Java: Object-Oriented Programming Beyond OOP Here's an interesting article about going beyond OOP: The Next Move in Programming: A Conversation with Sun's Victoria Livschitz at java.sun.com/developer/technicalArticles/Interviews
Java Object
Java Object         Object is the basic entity of object oriented programming language. Object.../java-object-oriented-language.shtml  
What is attribute oriented programming?
What is attribute oriented programming?  Hi, What is attribute oriented programming? Thanks
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
JavaScript Object Oriented Feature
; JavaScript supports the main principles of object-oriented programming...; </body> </html> Object-Oriented Programming The Object Oriented programming is a computer programming paradigm. Object Oriented
Java as an Object Oriented Language
applications and programs. OOP stands for Object Oriented Programming.... There are four main pillars of an Object Oriented Programming Language... of object oriented programming language. Class itself does nothing but the real
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
Object-Oriented Language: Java / APIs, Java OOPs
. Java is one of the useful Object Oriented programming language. Other Object..., Lasso, Perl 5,PHP5, VBScript, VBA etc. Java is popular object oriented programming...Java OOPs In this section we will learn Object Oriented (OOPs) Concepts 
(ii) String and String Buffer Class
between String and StringBuffer 1) String object is immutable
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
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
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
Method in Java
; In this section, we will explore the concept of method in the reference of object oriented programming techniques. As we earlier described in the topic class... in a class, object can't do anything without method. Some programming languages use
Object
are the basic units of the object-oriented programming. Objects are the part of our day... in terms of object-oriented programming. Objects are key to understanding object-oriented programming. Just look around and you'll find a lot of examples of real
Programming: Hammurabi II - Two Player Version
Java: Programming: Hammurabi II - Two Player Version Extend the basic Hammurabi project This is based on the Hammurabi I program, extending it to two... method to display the results. It creates one Kingdom object which it happens
Java7 : OOPs Concepts
JAVA7 : OOPs Concept In this section we will discuss concept of Object oriented programming. Object Oriented Programming : Object Oriented Programming, better termed as OOPs is a paradigm where everything is represented as an object
OOP Tutorial [first draft]
-Oriented Design (OOD) and Object-Oriented Programming (OOP) An important part... in the design process. In contrast to this, Object-Oriented Programming is concerned... = Object Oriented Programming Grouping related data with methods lets you not only
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
Introduction
the java applications and programs. OOP means Object Oriented Programming... Java as an Object Oriented Language   .... To be an Object Oriented language, any language must follow at least the four
Java Programming: Contents
: Fundamental Building Blocks of Programs Section 5: Objects and Object-oriented... Quiz on this Chapter Chapter 3: Programming in the Small II: Control... on this Chapter Chapter 5: Programming in the Large II: Objects and Classes
Java Programming: Chapter 5 Index
of object-oriented programming: inheritance and polymorphism. However... of the many details of object oriented programming in Java. Although these details... Chapter 5 Programming in the Large II Objects and Classes
Inheritance in java with example
Inheritance in java with example          Inheritance is one of the concept of the Object- Oriented programming. The advantage of using the inheritance
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
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
Object in java - Java Beginners
as a class object. Object: Object is the basic entity of object oriented.../java/master-java/java-object-oriented-language.shtml http...Object in java  Hi deepak, I am new to java, can u tell me what
java object - Java Beginners
of objects. The primitive data type and keyword void is work as a class object. Object: Object is the basic entity of object oriented programming language... their objects. Object is an instance of the class. It takes the properties (variables
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
Java Training and Tutorials, Core Java Training
Java is a powerful object-oriented programming language with simple code... interfaces because Java has built-in application programming interface (API) and due... errorless programming with platform independent feature. 
java object - Java Beginners
are stayed  Hi friend, Instance Variables (Non-static fields): In object oriented programming, objects store their individual states in the "non-static fields" that is declared without the static keyword. Each object of the class has its
programming
Java Constructor programming for single and double constructor  ... constructor,double parameter constor,and the now when we create a object of in this constructor he we tell the how much object we created in the no argument.single
Method
; In this section, we will explore the concept of method in the reference of object oriented... of method in java object-oriented technique first one is the Instance method...-oriented programming technique. It provide the facility to redefine the inherit
OOPs and Its Concepts in Java
;  Brief Introduction to OOP Object Oriented Programming... certain things common. In object oriented programming classes can inherit some common... procedural programming, here in the OOP programming model programs are organized
Object-Oriented Design - Overview
Java: Object-Oriented Design - Overview Encapsulation Article GetterEradicator It's a nice article about Encapsulation by Martin Fowler. Fowler is one of leading authors in the design area. His (with Kent Beck and other coauthors
Nested classes
Nested classes       Here is another advantage of the Java, an object-oriented programming... of nested classes, which are as under: i)  Static classes ii) Inner

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.