Question

Hello, this is java code. I'm practicing my Java and add comments if possible for explanation....

Hello, this is java code. I'm practicing my Java and add comments if possible for explanation.

Question 1:

Create an java program that has a class called Square. Create a function called squareParameter that calculates the parameter of the sides of the square. Write test methods in main() to test for different output.

Question 2:

Create a class called CarYearMade that contains a function called yearMade. This function receives an arraylist of different years. The function should return another arraylist that have the years from the original arraylist passed to it that show years only after the last number 1990. For example:

original arraylist yearMade([1985, 1987, 1990, 1994, 1990, 1999, 1998]) it should return [1999, 1998] only because there are two numbers 1990 but only after the last 1990 number, the values are 1999, 1998.

The constructor is an empty CarYearMade() and the yearMade function is public yearMade(CarYearMade list). It should return the list of integers only with the values from the original arraylist that are after the last number 1990. If passed arraylist is empty, return an empty arraylist. Create test values in main() in order to test.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//i am assuming that you are asking for Perimeter

/* package codechef; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Square

{

//side of square

double side;

  

// this would be invoked while object

// of that class created.Constructore

Square(double side)

{

//set the value of side

this.side=side;

}

// calculates the parameter of the sides of the square

public double squareParameter()

{

return 4*side;

}

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

Square square =new Square(4.5);//Square of side 4.5

//calculates the Perimeter

double Perimeter =square.squareParameter();

//print the Perimeter

System.out.println(Perimeter);

Square square1 =new Square(452.5);//Square of side 452.5

//calculates the Perimeter

double Perimeter1 =square1.squareParameter();

//print the Perimeter

System.out.println(Perimeter1);

}

}

Google Chrome En (Mp4) Thu Feb 15 1:09 AM* M Inbox-2014csb103 x M Chegg-Explore nev x C Hello, This Is Java C xCode, Compile

/* package codechef; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class CarYearMade

{

// this would be invoked while object

// of that class created.Constructore

CarYearMade()

{

}

// return arraylist that have the years from the

//original arraylist passed to it that show years only after the last number 1990.

public ArrayList<Integer> yearMade(ArrayList<Integer> carYearMadeList)

{

ArrayList<Integer> ans=new ArrayList<Integer>();//Creating arraylist

//make Iterator for arraylist

Iterator itr=carYearMadeList.iterator();  

int pos=-1,i=0;

//iterate all value of arraylist and find last 1990 occurence position

while(itr.hasNext()){  

int year=(Integer)itr.next();

//increment the count

i++;

//if yer is 1990 then update position

if(year==1990)

{

pos=i;

}

}

i=0;

//make Iterator for arraylist

itr=carYearMadeList.iterator();

//add years only after the last number 1990

while(itr.hasNext()){  

int year=(Integer)itr.next();

i++;

//if is valid ans then add into ans

if(year>1990 && i>pos)

{

ans.add(year);

}

}

//return required ans

return ans;

}

public static void main (String[] args) throws java.lang.Exception

{

// your code goes here

CarYearMade carYearMade =new CarYearMade();//make a object

ArrayList<Integer> carYearMadeList=new ArrayList<Integer>();//Creating input arraylist

//add year into ArrayList carYearMadeList

carYearMadeList.add(1985);

carYearMadeList.add(1987);

carYearMadeList.add(1990);

carYearMadeList.add(1994);

carYearMadeList.add(1990);

carYearMadeList.add(1999);

carYearMadeList.add(1998);

ArrayList<Integer> ans=new ArrayList<Integer>();//Creating answer arraylist

ans=carYearMade.yearMade(carYearMadeList);

//make Iterator for arraylist

Iterator itr=ans.iterator();

//iterate all value of arraylist

while(itr.hasNext()){

System.out.println(itr.next());  

}

}

}

Google Chrome En 4)) Thu Feb 15 1:29 AM* M Inbox-2014csb103 x M Chegg-Explore nev x C Hello, This is Java c x Code, Compile &

Add a comment
Know the answer?
Add Answer to:
Hello, this is java code. I'm practicing my Java and add comments if possible for explanation....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an...

    (intro to java help?) Write a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values: ("squid", "octopus") And you make the following call: manyStrings(list, 2); Then list should store the following values after the call: ("squid", "squid", "octopus", "octopus") As another example, suppose that list contains the following: ("a",...

  • JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free...

    JUnit5 JAVA. Need help to make a unit test of my GenericStack.java code below. Feel free to change the code below to fit the task better. thanks :) Assigment Create a new version of GenericStack (Have started on the code below) that uses an array instead of an ArrayList (this version should also be generic). Be sure to check the size of the array before adding a new item; - if the array becomes full, double the size of the...

  • Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm...

    Java In your own custom ArrayList<E> class(do not use Java Array List API) use Array algorithm Write a static function called RemoveNullElements() without creating another array. that remove Null elements and shift all the rest of the elements to the left/front (small indexes) of the array without changing the size or length of the original array( this meant the front of the array will have all the not Null elements, and the other haft will be empty/Null. Please provide test...

  • Please code this in java: The purpose of this assignment is to: Read double data in...

    Please code this in java: The purpose of this assignment is to: Read double data in to a TreeSet where each datum inserted is Math.abs(datum) (i.e., no negative values are inserted). NOTE: This will (i) sort the data placed into the TreeSet, and, (ii) remove duplicate values since no duplicates are allowed (i.e., duplicates will not be inserted). Create a list iterator. Iterate backwards through the TreeSet (i.e., from the largest value to the smallest) outputting all elements * -1.0....

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at...

    In java---- The DeckTester.java file, provides a basic set of Deck tests. Add additional code at the bottom of the main method to create a standard deck of 52 cards and test the shuffle method ONLY in the Deck class. After testing the shuffle method, use the Deck toString method to “see” the cards after every shuffle. Deck: import java.util.List; import java.util.ArrayList; /** * The Deck class represents a shuffled deck of cards. * It provides several operations including *...

  • JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include...

    JAVA Write Java code for extending the LinkedList class of java.util.* to ExtLinkedList that would include the following method: public ExtLinkedList firstHalfList() which returns the first half of the list. In other words, if you have a ExtLinkedList of size 5, for example, the method firstHalfList should return the list with the first two nodes. If it is a list of size 6, it should return the list with the first three nodes. The original list should not be modified....

  • Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h...

    Add a member function template called RemoveRandom to the ArrayBag class (so add it to ArrayBag.h). The RemoveRandom member function should remove a random entry from the list (so use your srand and rand functions). Do not forget to TEST... that means you will need to add the prototype and test with a simple main. All you need to submit though is the member function implementation Add an overloaded constructor to the ArrayBag class (so add it to 2. ArrayBag.h)....

  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT