Commit b38f7486 authored by indiana.brown's avatar indiana.brown

OOP example commit.

parent 3c67903d
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
public class LinearSearch{
public static void Main(String[] args){
StreamReader file = new StreamReader("million.txt");
string line;
string search;
string[] separator = {"<SEP>"};
string artist;
string title;
//List<string> songs = new List<string>();
List<Song> songs = new List<Song>();
//Writes file to memory
// while((line = file.ReadLine()) != null){
// string[] parts = line.Split(separator, StringSplitOptions.None);
// artistsong = (parts[2] + " - " + parts[3]);
// songs.Add(artistsong);
// }
while((line = file.ReadLine()) != null){
string[] parts = line.Split(separator, StringSplitOptions.None);
artist = parts[2];
title = parts[3];
songs.Add(new Song(title, artist));
}
Stopwatch swatch = new Stopwatch();
//Prompts user input
Console.WriteLine("Enter a song name: ");
search = Console.ReadLine();
//Returns results based on user input
Console.WriteLine("\nResults for " + search + ":");
swatch.Start();
foreach (Song s in songs) {
if (s.title == search) {
Console.WriteLine("\n" + s.artist + " - " + s.title);
}
}
swatch.Stop();
//Prints elapsed time for searching
Console.WriteLine("\nTime elapsed : {0} milliseconds", swatch.ElapsedMilliseconds);
// while((line = file.ReadLine()) != null){
// string[] parts = line.Split(separator, StringSplitOptions.None);
// foreach (var part in parts){
// Console.WriteLine(part);
// }
//Console.ReadLine();
// Console.WriteLine("Read line: " + );
// Console.ReadLine();
//}
}
}
using System;
using System.IO;
using System.Collections.Generic;
public class Song{
public Song(string title_, string artist_){
// line = line_;
title = title_;
artist = artist_;
}
// public string line;
public string title;
public string artist;
// string[] separator = {"<SEP>"};
// List<Song> songs = new List<Song>();
// public static void memAdd(){
// while((line = file.ReadLine()) != null){
// string[] parts = line.Split(separator, StringSplitOptions.None);
// foreach (var part in parts){
// songs.Add(new Song(title, artist));
// Console.WriteLine(part);
// }
// }
// }
};
This source diff could not be displayed because it is too large. You can view the blob instead.
public class Main{
public static void main(String[] args){
//Creates a new instance of the class Meme.
//The Meme() method takes three arguments as you can see when looking at
//the constructor for it.
Meme beAdvised = new Meme("Be Advised", 100, true);
Meme trollFace = new Meme("Troll Face", 25, false);
//Calls the whatMeme() method specifically for the beAdvised meme.
//The method knows what the meme is called because of the Meme() constructor.
//So long as you keep creating new instances of a class, passing the
//constructor the approproate arguments, you can have infinite memes!
beAdvised.whatMeme();
trollFace.whatMeme();
}
}
public class Meme{
//Class attributes
public String name;
public int memeScore;
public boolean isRelevant;
//Constructor method for meme. The variables used as arguments for this method
//are *temporary* variables and are only ever referred to here. It doesn't
//matter what they are called, as long as they are the variable types that you
//want.
public Meme(String n, int mS, boolean iR){
//Here you're converting the temporary variables that you get when a new class
//instance is created elsewhere (the Main for example) into the useable attributes
//of the class, the ones at the top.
//Any time a variable is referred to as "this.variable" it is referring to the variable
//of ONE specific instance of a class. So "this.name" for beAdvised is "Be Advised" and
//for trollFace it's "Troll Face".
this.name = n;
this.memeScore = mS;
this.isRelevant = iR;
}
//Methods can be called outside of this class with classInstanceName.methodName() - eg beAdvised.whatMeme();
public void whatMeme(){
//Again we're using this.variable to refer to the variable of ONE particular instance of this class
System.out.println("\nThis meme is called " + this.name + ".");
System.out.println("Half of this meme's score is " + this.memeScore/2 + ".");
if(this.isRelevant){
System.out.println("I'm happy to announce that this meme is relevant.");
}
else{
System.out.println("Seems you've got a dirty, irrelevant meme here, son.");
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment