|
Thx Sir, In the question there are different case like "ababacc", "aba","a" So what i am doing is i split the words into an array and then add the words as per the letter. there is one more approach that i want to ask will that will more efficiently or not
So i will divide the n from the array size of the string and then (string.length- reminder) and I will have the no of times it is divisible and i will also calculate the no of "a" present in the word
|
|
|
|
|
Like I said, there is no need to split the string. Just count the number of 'a's in the string. From that number, the length of the string, and the repeat count, you can easily calculate the total.
|
|
|
|
|
|
No post
modified 13-Jan-20 9:36am.
|
|
|
|
|
|
Many people would want to help but we're all pretty bad at mind reading. At least most people on this site think that about me.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
I think I know what you are thinking now ...
|
|
|
|
|
hello guys!
I need a java project on BUS RESERVATION AND TICKETTING SYSTEM done by object oriented concepts.
so if any one to help me please send me a source code!
|
|
|
|
|
Sorry, but this site does not provide code to order.
|
|
|
|
|
Member 14697150 wrote: please send me a source code! Nope. And it's incredibly rude of you to even ask for that.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
a java project on us reservation sytem
|
|
|
|
|
Yes, that would be a good project.
|
|
|
|
|
Hi guys, this is my code but I got some error. Anyone can help me?
I have an error in executer part.
public class ReaderWriter{
public static void main(String args[]){
ReadWriteLock RW = new ReadWriteLock();
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
}
}
class Writer implements Runnable
{
private ReadWriteLock rwlock;
private int writerNum;
public Writer(int w, ReadWriteLock rw) {
writerNum = w;
rwlock = rw;
}
public void run() {
while (true){
SleepUtilities.nap();
System.out.println("writer " + writerNum + " wants to write.");
rwlock.WriteLock(writerNum);
SleepUtilities.nap();
rwlock.WriteUnLock(writerNum);
}
}
}
class Reader implements Runnable
{
private ReadWriteLock rwlock;
private int readerNum;
public Reader(int readerNum, ReadWriteLock database) {
this.readerNum = readerNum;
this.rwlock = database;
}
public void run() {
while (true) {
SleepUtilities.nap();
System.out.println("reader " + readerNum + " wants to read.");
rwlock.ReadLock(readerNum);
SleepUtilities.nap();
rwlock.ReadUnLock(readerNum);
}
};
}
class ReadWriteLock {
private int readerCount;
private Semaphore mutex;
private Semaphore rwlock;
public ReadWriteLock() {
readerCount = 0;
mutex = new Semaphore(1);
rwlock = new Semaphore(1);
}
public void ReadLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {
}
++readerCount;
if (readerCount == 1){
try{
rwlock.acquire();
}
catch (InterruptedException e) {
}
}
System.out.println("Reader " + readerNum + " is reading. Reader count = " + readerCount);
mutex.release();
}
public void ReadUnLock(int readerNum) {
try{
mutex.acquire();
}
catch (InterruptedException e) {}
--readerCount;
if (readerCount == 0){
rwlock.release();
}
System.out.println("Reader " + readerNum + " is done reading. Reader count = " + readerCount);
mutex.release();
}
public void WriteLock(int writerNum) {
try{
rwlock.acquire();
}
catch (InterruptedException e) {}
System.out.println("Writer " + writerNum + " is writing.");
}
public void WriteUnLock(int writerNum) {
System.out.println("Writer " + writerNum + " is done writing.");
rwlock.release();
}
}
class SleepUtilities
{
public static void nap() {
nap(NAP_TIME);
}
public static void nap(int duration) {
int sleeptime = (int) (NAP_TIME * Math.random() );
try { RW.sleep(sleeptime*1000); }
catch (InterruptedException e) {}
}
private static final int NAP_TIME = 5;
}
|
|
|
|
|
So you have an error somewhere in your code. Do you not think that it would help if you explained exactly what the error is, and which line of code it occurs on?
|
|
|
|
|
The constructor Writer(ReadWriteLock) is undefined
The constructor Reader(ReadWriteLock) is undefined
I have this errors. I made a mistake in main part but I dont know how can I fix it?
|
|
|
|
|
The issue is that the constructor you are using for your reader and writer objects is not defined: you have defined a constructor for each of these classes which takes an integer as first argument, and a readerwriterlock object as second argument, but you actually call non-existant constructors accepting only a readerwriterlock as single argument.
You can either:- define constructors accepting as single readerwriterlock object as argument;
- change your actual constructor calls and add the integer argument which you seem to have forgotten.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I rewrited main part like this and it worked. But I want to write it with using executer services. But in that way still it is not working.
public class ReaderWriter{
public static final int NUM_OF_READERS = 3;
public static final int NUM_OF_WRITERS = 2;
public static void main(String args[]){
ReadWriteLock rwlock = new ReadWriteLock();
Thread[] readerArray = new Thread[NUM_OF_READERS];
Thread[] writerArray = new Thread[NUM_OF_WRITERS];
for (int i = 0; i < NUM_OF_READERS; i++) {
readerArray[i] = new Thread(new Reader(rwlock));
readerArray[i].start();
}
for (int i = 0; i < NUM_OF_WRITERS; i++) {
writerArray[i] = new Thread(new Writer(rwlock));
writerArray[i].start();
}
}
Also like you said I wrote my functions which take just one argument and removed SleepUtlization class.
class Reader implements Runnable
{
private static int readers = 0;
private ReadWriteLock rwlock;
private int readerNum;
public Reader(ReadWriteLock rwlock) {
this.rwlock = rwlock;
this.readerNum=Reader.readers++;
}
public void run() {
while (true) {
final int DELAY = 5000;
try
{
Thread.sleep((int) (Math.random() * DELAY));
}
catch (InterruptedException e) {
}
System.out.println("reader " + readerNum + " wants to read.");
rwlock.ReadLock(readerNum);
try
{
Thread.sleep((int) (Math.random() * DELAY));
}
catch (InterruptedException e) {
}
rwlock.ReadUnLock(readerNum);
}
};
}
|
|
|
|
|
pembepanter wrote: But in that way still it is not working. Yet another piece of non-information. One of the most frustrating things on CodeProject is people (like you) who post messages like, "I received an error", "my code is not working", "I tried but got nothing", etc. None of these statements tell us anything about your problem, so you leave us to guess what you are doing wrong, which is generally not possible.
|
|
|
|
|
I'm new at like these forums and about coding. I've written about this topic before. I had no information about this project. And you said me its your project do it via google. I researched and I wrote this code from the beginnig.
So I had an error and I asked people who can try to help me and answer my questions (not like you). You break my enthusiasm.
|
|
|
|
|
pembepanter wrote: So I had an error But you don't tell us what the error is. How can we guess, as we cannot see your screen, or read your mind? If you call the doctor and say, "I have a pain", do you expect him to know what type of pain, or where it is?
We are all trying to help answer these questions, but we cannot do it without the proper information.
|
|
|
|
|
the ReaderWriter is a class the can be used together with outStreamReader to write data to the file in java programming languange.
|
|
|
|
|
Looks like a pointless exercise. Not much interest in that.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
yeah but it is my school project 
|
|
|
|
|
Hi I have a project but I do not have an an idea about the topic. Can you help me or give any resources about that topic?
This is my project:
In this project you will need to provide a solution to readers-writers problem in which several processes (readers and writers) are trying to access shared variables. Obviously, if two readers access the shared data simultaneously, no adverse effects will result, hence, they are allowed to access. However, if a writer and some other process (either a reader or a writer) access the data simultaneously, chaos may ensue. To ensure that these difficulties do not arise, we require that the writers have exclusive access to the shared data while writing to the data.
The solution must guarantee that:
If a writer has begun writing process, then
No additional writer can perform write function
No reader is allowed to read
If 1 or more readers are reading, then
Other readers may read as well
No writer may perform write function until all readers have finished reading
You are given Test class that use ReadWriteLock class and threads for the problem. You are expected to use Semaphore provided in the code.
Two operations on the semaphore is allowed; acquire() and release()
To do:
Implement methods of ReadWriteLock class given.
Also this my code test.java
package sync;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
public class Test {
public static void main(String [] args) {
ExecutorService executorService = Executors.newCachedThreadPool();
ReadWriteLock RW = new ReadWriteLock();
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Writer(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
executorService.execute(new Reader(RW));
}
}
class ReadWriteLock{
private Semaphore S=new Semaphore(1);
public void readLock() {
}
public void writeLock() {
}
public void readUnLock() {
}
public void writeUnLock() {
}
}
class Writer implements Runnable
{
private ReadWriteLock RW_lock;
public Writer(ReadWriteLock rw) {
RW_lock = rw;
}
public void run() {
while (true){
RW_lock.writeLock();
RW_lock.writeUnLock();
}
}
}
class Reader implements Runnable
{
private ReadWriteLock RW_lock;
public Reader(ReadWriteLock rw) {
RW_lock = rw;
}
public void run() {
while (true){
RW_lock.readLock();
RW_lock.readUnLock();
}
}
}
Thank you
|
|
|
|
|
Firstly, this is your homework so you are expected to (at least try to) do the work.
Secondly, you have not asked an actual question.
You could start by doing some research, via Google, on the issues that you do not understand. Then, when you have a specific question, people will try to help you.
|
|
|
|