Question

If possible, write this code in object oriented programming, if not in object oriented that's okay...

If possible, write this code in object oriented programming, if not in object oriented that's okay I can convert it. All in C++ language

Generate a list of random numbers using srand and rand, sort the random numbers and put the sorted list into a text file (.txt) using for loop and use stream insertion operator to a text file. Also, put it in a binary file (not using a for loop or stream insertion [use right system caller]). All arrays must be dynamic (in pointer notation, nothing in brackets) and not static. Show null pointers. Treat every number as a radius, so write a class header file to calculate the circumference of the circle and area.

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

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.PrintWriter;

import java.util.Arrays;

import java.util.Random;

public class Tester {

public static void main(String[] args) {

  

Random rand = new Random();

int randomArray[] = new int[10];

//Generating the random array with limit from 0-100

for(int i=0; i<randomArray.length;i++){

randomArray[i] = rand.nextInt(100);

}   

//Sorting the array

Arrays.sort(randomArray);

  

//Writing to text file

try {

//Using fileWriter and printWriter to write the random number to text file

FileWriter fileWriter = new FileWriter("C:\\Srini\\Random.txt");

PrintWriter printWriter = new PrintWriter(fileWriter);

//using for each loop to write the array to text file

for(int i:randomArray){

printWriter.println(i);

}

printWriter.close();

fileWriter.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  

  

//Writing the random numbers to binary file

try {

//Using ObjectOutputStream to write the array to binary file.

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Srini\\Random.dat"));

oos.writeObject(randomArray);

oos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  

//Printing the circumference and area of the random radii

for(int i:randomArray){

System.out.println("Radius: "+ i +"\tCircumference: "+getCircumference(i)+"\tArea: "+getArea(i));

}

}

private static double getCircumference(int radius) {

// TODO Auto-generated method stub

double circumference = (double)(2 * Math.PI * radius);

return circumference;

}

private static double getArea(int radius) {

// TODO Auto-generated method stub

double area =(double)(Math.PI * radius * radius);

return area;

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
If possible, write this code in object oriented programming, if not in object oriented that's okay...
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
  • Project Lists and Object-oriented Programming Note: using c++ This project is an individual assignment that focuses...

    Project Lists and Object-oriented Programming Note: using c++ This project is an individual assignment that focuses on object-oriented programming and lists. The list will be a doubly-linked list and it will be implemented using nodes and pointers. The data contained in the list will be objects from a class. Project data: You will choose one of the following classes to define: Boat, Earthquake, Game, Sport, State, Website, House, Phone, Activist, Plant, Fish, Parrot, and Course. Your class code will include...

  • In this lab you will convert lab5.py to use object oriented programming techniques. The createList and...

    In this lab you will convert lab5.py to use object oriented programming techniques. The createList and checkList functions in lab5.py become methods of the MagicList class, and the main function of lab6.py calls methods of the MagicList class to let the user play the guessing game.                              A. (4pts) Use IDLE to create a lab6.py. Change the 2 comment lines at the top of lab6.py file: First line: your full name Second line: a short description of what the program...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented...

    *Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows:  For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method.  For each...

  • In object-oriented programming, the object encapsulates both the data and the functions that operate on the...

    In object-oriented programming, the object encapsulates both the data and the functions that operate on the data. True False Flag this Question Question 101 pts You must declare all data members of a class before you declare member functions. True False Flag this Question Question 111 pts You must use the private access specification for all data members of a class. True False Flag this Question Question 121 pts A private member function is useful for tasks that are internal...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • Using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; u...

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace FileWriter { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCreate_Click(object sender, EventArgs e) { try { //write code that assigns the value in the textbox to an Integer variable if // write code that validates whether or not the data entered in the textbox is greater than or equal to 1 *...

  • object oriented programming Jaya Write a complete program using function/Method named Average to calculate of four...

    object oriented programming Jaya Write a complete program using function/Method named Average to calculate of four numbers entered by user (treat the four variables as local in main). Read the four numbers in main function and send them as parameter to the function Average. (Marks are allocated to function prototype, function call and function definition).

  • The purpose of this project is to give students more exposure to object oriented design and...

    The purpose of this project is to give students more exposure to object oriented design and programming using classes and polymorphism in a realistic application that involves arrays of objects and sorting arrays containing objects A large veterinarian services many pets and their owners. As new pets are added to the population of pets being serviced, their information is entered into a flat text file. Each month the vet requests and updates listing of all pets sorted by their "outstanding...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

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