In: Computer Science
programming language is a formal language comprising a set of instructions that produce various kinds of output. Programming languages are used in computer programming to implement algorithms.import java.util.*;//imported util package
A website is a collection of web pages and related content that is identified by a common domain name and published on at least one web server. Notable examples are wikipedia.org, google.com, and amazon.com.
All publicly accessible websites collectively constitute the World Wide Web. There are also private websites that can only be accessed on a private network, such as a company's internal website for its employees.
Websites are typically dedicated to a particular topic or purpose, such as news, education, commerce, entertainment, or social networking. Hyperlinking between web pages guides the navigation of the site, which often starts with a home page.
A Uniform Resource Locator (URL), colloquially termed a web address,is a reference to a web resource that specifies its location on a computer network and a mechanism for retrieving it. A URL is a specific type of Uniform Resource Identifier (URI), although many people use the two terms interchangeably. URLs occur most commonly to reference web pages (http), but are also used for file transfer (ftp), email (mailto), database access (JDBC), and many other applications.
Most web browsers display the URL of a web page above the page in an address bar.
import java.io.*;//imported io package
class TutorialWebsite implements Comparable<TutorialWebsite>{//TutorialWebsite class
String LanguageName;
String WebsiteDescription;
String WebsiteURL;
TutorialWebsite(String name, String description, String url){
LanguageName = name;
WebsiteDescription = description;
WebsiteURL = url;
}
public int compareTo(TutorialWebsite tw)
{
if(LanguageName == tw.LanguageName)
return 0;
else if(LanguageName.compareTo(tw.LanguageName)>0)
return 1;
else return -1;
}
}
class Main{/*main class named Main*/
/* descending write method writes the data into Descending.csv file by taking arrayList as argument*/
void descendingwrite(ArrayList<TutorialWebsite> arr)throws IOException{
DataOutputStream dos= new DataOutputStream(new FileOutputStream("Descending.csv"));
for(TutorialWebsite tutorial:arr)
{
dos.writeChars(tutorial.LanguageName);
dos.writeChars("\n");
dos.writeChars(tutorial.WebsiteDescription);
dos.writeChars("\n");
dos.writeChars(tutorial.WebsiteURL);
dos.writeChars("\n");
}
dos.close();
}
void ascendingwrite(ArrayList<TutorialWebsite> arr)throws IOException
{
DataOutputStream dos= new DataOutputStream(new FileOutputStream("Ascending.csv"));
for(TutorialWebsite tutorial:arr)
{
dos.writeChars(tutorial.LanguageName);
dos.writeChars("\n");
dos.writeChars(tutorial.WebsiteDescription);
dos.writeChars("\n");
dos.writeChars(tutorial.WebsiteURL);
dos.writeChars("\n");
}
dos.close();
}
void selectionSort()throws IOException{/*selectionSort method, sorts the list in ascending order*/
File file = new File("Tutorial.txt");
Scanner read = new Scanner(file);/*created an object of Scanner class named input*/
ArrayList<TutorialWebsite> arr = new ArrayList<TutorialWebsite>();
while(read.hasNext()){
arr.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));
}
int n = arr.size();
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// In unsorted array find the minimum element
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr.get(j).LanguageName.compareTo(arr.get(min_idx).LanguageName)<0)
min_idx = j;
// Swap the found minimum element with the first
// element
Collections.swap(arr,i,min_idx);
}
ascendingwrite(arr);/*called the method ascendingwrite by passing arr*/
System.out.println("written into Ascending file successfully");/*if the above descendingwrite method works fine then this statement is printed*/
}
void insertionSort()throws IOException{/*insertionSort method, sorts the list in descending order*/
File file = new File("Tutorial.txt");
Scanner read = new Scanner(file);/*created an object of Scanner class named input*/
ArrayList<TutorialWebsite> arr = new ArrayList<TutorialWebsite>();
while(read.hasNext()){
arr.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));
}
int n = arr.size();
for (int i=1; i<n; ++i)
{
String name = arr.get(i).LanguageName;
String description = arr.get(i).WebsiteDescription;
String url = arr.get(i).WebsiteURL;
int j = i-1;
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */
while (j>=0 && arr.get(j).LanguageName.compareTo(name)<0)
{
arr.set(j+1,arr.get(j));
j = j-1;
}
arr.set(j+1,new TutorialWebsite(name, description, url));
}
descendingwrite(arr);/*called the method descendingwrite by passing arr*/
System.out.println("written into Descending file successfully");/*if the above descendingwrite method works fine then this statement is printed*/
}
public static void main(String args[])throws IOException{/*main method, execution of program starts from here*/
File file = new File("Tutorials.txt");/*creates a file object by passing the file name Tutorials.txt*/
Scanner read = new Scanner(file);/*created an object of Scanner class named read*/
ArrayList<TutorialWebsite> tw = new ArrayList<TutorialWebsite>();
while(read.hasNext()){/*reads the data into ArrayList named tw*/
tw.add(new TutorialWebsite(read.nextLine(), read.nextLine(), read.nextLine()));
}
Collections.sort(tw);//sorted using comparable interface.
Main main = new Main();/*created an object to Main class to call to selectionSort and insertionSort methods*/
main.insertionSort();/*a call to insertionSort is invoked*/
main.selectionSort();/*a call to selectionSort is invoked*/
}
}